Update Dimensions for the Item,Customer and Vendor

Some times you may get a strange requirement from client to update few dimensions on Default Dimensions :

Below code snippet is used to update the required dimensions without disturbing the other dimensions


//Bellow example will remove the BU,Dept and CC without distrubing the other dimensions
static void ItemDimUpdate(Args _args)
{
    InventTable inventTable,inventTableselect;
    container dimValue;
    DimensionDefault DimensionDefault,DimensionDefault1;
    str BussinessUnit,CostCenter,Department,ItemGroup,Project;
    int i = 0;

    DimensionAttributeValueSetStorage       dimStorage;
    CustTable                               custTable;
    Counter                                 c;
    

    ttsBegin;
    while select * from inventTableselect
    {
         i = 0;
         BussinessUnit = "";
         CostCenter = "";
         Department = "";
         ItemGroup = "";
         Project = "";
         dim = new Struct();
         dimValue = connull();

        //Getting values from the Old Dimension
        dimStorage = DimensionAttributeValueSetStorage::find(inventTableselect.DefaultDimension);
        for (c=1 ; c<= dimStorage.elements() ; c++)
        {
            switch (DimensionAttribute::find(dimStorage.getAttributeByIndex(c)).Name)
            {
                case 'BussinessUnit':
                    BussinessUnit = dimStorage.getDisplayValueByIndex(c);
                break;
                case 'CostCenter':
                    CostCenter = dimStorage.getDisplayValueByIndex(c);
                break;
                case 'Department':
                    Department = dimStorage.getDisplayValueByIndex(c);
                break;
                case 'ItemGroup':
                    ItemGroup = dimStorage.getDisplayValueByIndex(c);
                break;
                case 'Project':
                    Project = dimStorage.getDisplayValueByIndex(c);
                break;
            }
        }

       //Creating the New Dimension
       //Just keep the required dimensions and Comment the other dimensions

       // if(BussinessUnit)
       //     dim.add('BusinessUnit', BussinessUnit);
       // if(CostCenter)
       //     dim.add('Costcenter', CostCenter);
       // if(Department)
       //     dim.add('Department', Department);
        if(ItemGroup)
            dim.add('ItemGroup', ItemGroup);
        if(Project)
            dim.add('Project', Project);

        dimValue += dim.fields();

      //  if(BussinessUnit)
      //  {
      //      i++;
      //      dimValue += dim.fieldName(i);
      //      dimValue += dim.valueIndex(i);
      //  }
      //  if(CostCenter)
      //  {
      //      i++;
      //      dimValue += dim.fieldName(i);
      //      dimValue += dim.valueIndex(i);
      //  }
      //  if(Department)
      //  {
      //      i++;
      //      dimValue += dim.fieldName(i);
      //      dimValue += dim.valueIndex(i);
      //  }
        if(ItemGroup)
        {
            i++;
            dimValue += dim.fieldName(i);
            dimValue += dim.valueIndex(i);
        }
        if(Project)
        {
            i++;
            dimValue += dim.fieldName(i);
            dimValue += dim.valueIndex(i);
        }

        //Below code will get the new dimension
        DimensionDefault = AxdDimensionUtil::getDimensionAttributeValueSetId(dimValue);

        inventTable = inventTable::find(inventTableselect.ItemId,true);
        inventTable.DefaultDimension = DimensionDefault;

        inventTable.update();
    }
    ttsCommit;

    info("Updation completed");
}

 

Below Code snippet is used to update the Item,Customer and Vendor :


static void ItemDimUpdate(Args _args)
{
    InventTable inventTable;
    Struct dim = new Struct();
    container dimValue;
    DimensionDefault DimensionDefault;
    ;  

    dim.add('BusinessUnit', '002');
    dim.add('CostCenter', '007');
    dim.add('Department', '022');
    dim.add('ItemGroup', 'Audio Test');
    dim.add('Project', '000002');  

    dimValue += dim.fields();
    dimValue += dim.fieldName(1);
    dimValue += dim.valueIndex(1);
    dimValue += dim.fieldName(2);
    dimValue += dim.valueIndex(2);
    dimValue += dim.fieldName(3);
    dimValue += dim.valueIndex(3);
    dimValue += dim.fieldName(4);
    dimValue += dim.valueIndex(4);
    dimValue += dim.fieldName(5);
    dimValue += dim.valueIndex(5);  

    ttsBegin;  

    DimensionDefault = AxdDimensionUtil::getDimensionAttributeValueSetId(dimValue);
    inventTable = inventTable::find("A0001",true);
    inventTable.DefaultDimension = DimensionDefault;
    inventTable.update();  

    ttsCommit;  

}

Import Customer through X++


static void UploadCustomer(Args _args)
{
 CommaTextIO csvFile;
 container readCon;
 counter icount,inserted;
 Dialog dialog;
 DialogField dfFileName;


 FileName fileName;
 Name name;

 DirPartyContactInfoView contactView;
 CustTable CustTable;
 DirParty dirParty;
 DirPartyRecId partyRecId;

 LogisticsPostalAddress address;
 DirPartyPostalAddressView addressView;

 inserted =0;

 #File


 dialog = new Dialog("Pick the file");
 dfFileName = dialog.addField(extendedTypeStr("FilenameOpen"));
 dialog.filenameLookupFilter(["All files", #AllFiles]);


 if (dialog.run())
 {
 csvFile = new CommaTextIo(dfFileName.value(), 'r');
 csvFile.inFieldDelimiter(',');
 readCon = csvFile.read();
 ttsBegin;
 while(csvFile.status() == IO_Status::OK)
 {
 readCon = csvFile.read();

 if(readCon)
 {
 icount++;

 name = conPeek(readCon,2);
 partyRecId = DirPartyTable::createNew( DirPartyType::Organization, name).RecId;

 custTable.clear();
 custTable.initValue();
 custTable.Party = partyRecId;
 custTable.AccountNum = conPeek(readCon,1);
 custTable.CustGroup = conPeek(readCon,3);
 custTable.Currency = conPeek(readCon,7);
 custTable.DlvMode = conPeek(readCon,8);
 custTable.PaymTermId = conPeek(readCon,9);
 custTable.insert();

 custTable = Custtable::find(conPeek(readCon,1));
 partyRecId = custTable.Party;

 DirParty = DirParty::constructFromPartyRecId(partyRecId );
 address.clear();
 //address.PostBox = strLRTrim(conPeek(readCon,13));
 address.CountryRegionId = strLRTrim(conPeek(readCon,5));
 if( address.CountryRegionId != "")
 {
 address.State = strLRTrim(conPeek(readCon,15));
 address.ZipCode = strLRTrim(conPeek(readCon,15));
 address.Street = strLRTrim(conPeek(readCon,4));
 address.City = strLRTrim(conPeek(readCon,18));
 
 //address.Address = conPeek(readCon,4);
 
 addressView.LocationName = "Address";
 addressView.IsPrimary = NoYes::Yes;
 addressView.Party = partyRecId;
 addressview.initFromPostalAddress(address);


 DirParty = DirParty::constructFromPartyRecId(addressView.Party );
 DirParty.createOrUpdatePostalAddress(addressView);
 }

 contactView.clear();

 if(conPeek(readCon,5) != "")
 {
 contactView.LocationName = "Phone Number";
 contactView.Locator = strLRTrim(conPeek(readCon,5));
 contactView.Type = LogisticsElectronicAddressMethodType::Phone;
 contactView.Party = partyRecId;
 contactView.IsPrimary = NoYes::Yes;
 dirParty.createOrUpdateContactInfo(contactView);
 }

 if(conPeek(readCon,3) != "")
 {
 contactView.LocationName = "Fax Number";
 contactView.Locator = strLRTrim(conPeek(readCon,3));
 contactView.Type = LogisticsElectronicAddressMethodType::Fax;
 contactView.Party = partyRecId;
 contactView.IsPrimary = NoYes::Yes;
 dirParty.createOrUpdateContactInfo(contactView);
 }

 if(conPeek(readCon,4) != "")
 {
 contactView.LocationName = "Website";
 contactView.Locator = strLRTrim(conPeek(readCon,4));
 contactView.Type = LogisticsElectronicAddressMethodType::URL;
 contactView.Party = partyRecId;
 contactView.IsPrimary = NoYes::Yes;
 dirParty.createOrUpdateContactInfo(contactView);
 }

 if(conPeek(readCon,6) != "")
 {
 contactView.LocationName = "Email";
 contactView.Locator = strLRTrim(conPeek(readCon,6));
 contactView.Type = LogisticsElectronicAddressMethodType::Email;
 contactView.Party = partyRecId;
 contactView.IsPrimary = NoYes::Yes;
 dirParty.createOrUpdateContactInfo(contactView);
 }

 }

 }
 ttsCommit;
 }

}

Get Customer, Vendor& Company Addresses in AX 2012

 Get Customer,  Vendor& Company Addresses in AX 2012

//Get Customer/Vendor Address by Id & Current Comapny Address
//By replacing the CustTable with the VendTable we will get the address of Vendor
static void Job85(Args _args)
{
    CustTable custTable;
    str       description,address,completeaddress;
    
    custTable = CustTable::find('L0115');
          
    description = DirParty::primaryPostalAddress(custTable.Party).displayLocationDescription();
    
    address = DirParty::primaryPostalAddress(custTable.Party).Address;
    
    completeaddress = strFmt("%1,%2",description,address);
    info(strFmt("%1",completeaddress));
    
    //Get the Current Company Address
    info(strFmt("%1,%2",curext(),LogisticsPostalAddress::findByLocation(CompanyInfo::find().postalAddress().Location).Address));
    
}