Tuesday 21 April 2015

Update Vendor Contact Information - In Ax 2012

Hello Guys ..!

Here i'm sharing code to update vendor contact details (phone, fax, email) in  ax 2012.

Note :
-> create temp table (VendorImportTmp ) with VendorId,phone,fax,email fields
-> Use CSV file

static void Vendor_Contactsupdate(Args _args)
{
    CommaIO                             csvFile;
    container                           readCon;
    counter                             icount,inserted;
    container                           line;
    Dialog                              dialog;
    DialogField                         dfFileName;
    FileName                            fileName;
    VendAccount                         vendNum;
    DirOrganization                     dirOrganization;
    LogisticsLocation                   logisticsLocation;
    LogisticsPostalAddress              logisticsPostalAddress;
    LogisticsElectronicAddress          logisticsElectronicAddressPhone, logisticsElectronicAddressfax, LogisticsElectronicAddressEmail;
    VendTable                           vendTable;
    VendorImportTmp                     vendorImportTmp;
    DirPartyTable                       dirPartyTable, dirPartyTableLocal;
 

    inserted=0;
    #File
    dialog = new Dialog("Select File to Import");
    dfFileName = dialog.addField(extendedTypeStr(FileNameOpen));
    dialog.filenameLookupFilter(["All files", #AllFiles]);
    vendNum ="";
    if (dialog.run())
    {
    filename =  dfFileName.value();
    }
    csvFile = new CommaIO(filename, 'r');
       if (csvFile)
        {
            ttsBegin;
            while (csvFile.status() == IO_Status::OK)
            {
                readCon = csvFile.read();
                    if (readCon)
                    {
                            vendorImportTmp.clear();
                            vendorImportTmp.RecId =0;
                            vendorImportTmp.AccountNum            = conPeek(readCon,1);
                            vendorImportTmp.LocaterPhone           = conPeek(readCon,2);
                            vendorImportTmp.LocaterFax             = conPeek(readCon,3);
                            vendorImportTmp.LocaterEmail          = conPeek(readCon,4);
                            vendorImportTmp.ContactDescription = conPeek(readCon,5);
                            dirPartyTable = DirPartyTable::findRec(VendTable::find(vendorImportTmp.AccountNum).Party);
                            dirOrganization = dirPartyTable;
                                vendorImportTmp.Name = VendTable::find(vendorImportTmp.AccountNum).name();
                                            logisticsLocation = LogisticsLocation::create(vendorImportTmp.Name, NoYes::Yes);
                                        //Party location
                                        DirParty::addLocation(dirOrganization.RecId, logisticsLocation.RecId, true, true, false, [LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Business).RecId]);

                                        //Location
                                        logisticsLocation = LogisticsLocation::create('Electronic address', NoYes::Yes);

                                        //Party location
                                        DirParty::addLocation(dirOrganization.RecId, logisticsLocation.RecId, false, true, false);

                                        if(vendorImportTmp.LocaterPhone)
                                        {
                                            logisticsElectronicAddressPhone.clear();
                                            logisticsElectronicAddressPhone.initValue();
                                            logisticsElectronicAddressPhone.Location = logisticsLocation.RecId;
                                            logisticsElectronicAddressPhone.Type = LogisticsElectronicAddressMethodType::Phone;
                                            logisticsElectronicAddressPhone.Locator = vendorImportTmp.LocaterPhone;
                                            if(vendorImportTmp.ContactDescription == "")
                                            {
                                                vendorImportTmp.ContactDescription = "Phone";
                                            }
                                            logisticsElectronicAddressPhone.Description = vendorImportTmp.ContactDescription;
                                            logisticsElectronicAddressPhone.IsPrimary = NoYes::Yes;

                                            if (logisticsElectronicAddressPhone.validateWrite())
                                            {
                                                logisticsElectronicAddressPhone.insert();
                                            }
                                        if(vendorImportTmp.LocaterFax)
                                        {
                                            logisticsElectronicAddressfax.clear();
                                            logisticsElectronicAddressfax.initValue();
                                            logisticsElectronicAddressfax.Location = logisticsLocation.RecId;
                                            logisticsElectronicAddressfax.Type = LogisticsElectronicAddressMethodType::Fax;
                                            logisticsElectronicAddressfax.Locator = vendorImportTmp.LocaterFax;
                                             if(vendorImportTmp.ContactDescription == "")
                                            {
                                                vendorImportTmp.ContactDescription = "fax";
                                            }
                                            logisticsElectronicAddressfax.Description = vendorImportTmp.ContactDescription;
                                            logisticsElectronicAddressfax.IsPrimary = NoYes::Yes;
                                            if (logisticsElectronicAddressfax.validateWrite())
                                            {
                                                logisticsElectronicAddressfax.insert();
                                            }
                                        }
                                        if(vendorImportTmp.LocaterEmail)
                                        {
                                            LogisticsElectronicAddressEmail.clear();
                                            LogisticsElectronicAddressEmail.initValue();
                                            LogisticsElectronicAddressEmail.Location = logisticsLocation.RecId;
                                            LogisticsElectronicAddressEmail.Type = LogisticsElectronicAddressMethodType::Email;
                                            LogisticsElectronicAddressEmail.Locator = vendorImportTmp.LocaterEmail;
                                            LogisticsElectronicAddressEmail.Description = vendorImportTmp.ContactDescription;
                                            if (LogisticsElectronicAddressEmail.validateWrite())
                                            {
                                               LogisticsElectronicAddressEmail.insert();
                                            }
                                        select forUpdate dirPartyTableLocal where dirPartyTableLocal.RecId == dirPartyTable.RecId;
                                        if(dirPartyTableLocal)
                                        {
                                            dirPartyTableLocal.PrimaryContactPhone = logisticsElectronicAddressPhone.RecId;
                                            dirPartyTableLocal.PrimaryContactFax = logisticsElectronicAddressfax.RecId;
                                            dirPartyTableLocal.PrimaryContactEmail = LogisticsElectronicAddressEmail.RecId;
                                            dirPartyTableLocal.update();
                                        }
                                         
                                            inserted++;
                                        }
                        }

                    }
            }

    }
    info(strFmt("%1 Records inserted Succesfully",inserted));
    ttsCommit;
}


@Rahul Talasila

No comments:

Post a Comment

Adding a newline into a string in C# and X++

Below is the sample code we can use for  adding a newline after every occurrence of "@" symbol in the string in C#   using System...