Search This Blog

Monday, March 5, 2012

AX2012 Import Vendors from CSV using VENDVendTableService


The following sample is used to import the Data into Vendors from CSV using VENDVENDTABLEService.

Please change the container positions accordingly to your file.
static void Krishh_ImportVendorMaster(Args _args)
{
    VendVendTableService                        vendVendTableService;
    VendVendTable                               vendVendTable;
    VendVendTable_VendTable                     vendTable;
    VendVendTable_DirPartyPostalAddressView     adresss;
    VendVendTable_DirPartyTable_DirOrganiza1    dirPartyTable;
    VendVendTable_DirPartyContactInfoView       contactInfo;
    VendVendTable_OrganizationName              organizationName;
    TaxVATNumTable                              TaxVATNumTable;
    str                                         countryRegionId;
    CommaTextIo                 file;
    container                   rec;

    Dialog              d;
    DialogField         df1, df2;
    ;
    d = new Dialog("Import vendors");
    df1 = d.addField(ExtendedTypeStr("FilenameOpen"));

    if (d.run())
    {
        file = new CommaTextIo(df1.value(), 'r');
        file.inFieldDelimiter(';');

        vendVendTableService = vendVendTableService::construct();

        ttsBegin;

        while (file.status() == IO_Status::Ok)
        {
            rec = file.read();

            vendVendTable = new VendVendTable();
            vendTable = vendVendTable.createVendTable().addNew();

            vendTable.parmAccountNum(conPeek(rec, 1));
            vendTable.parmCurrency(conPeek(rec, 2));
            vendTable.parmName(conPeek(rec, 3));
            vendTable.parmVendGroup(conPeek(rec, 4));
            vendTable.parmVATNum(conPeek(rec, 11));

            countryRegionId = conPeek(rec, 6);

            if (!TaxVATNumTable::exist(vendTable.parmVATNum(), countryRegionId))
            {
                TaxVATNumTable.VATNum = vendTable.parmVATNum();
                TaxVATNumTable.CountryRegionId = countryRegionId;
                TaxVATNumTable.insert();
            }

            dirPartyTable = new VendVendTable_DirPartyTable_DirOrganiza1();
            //dirPartyTable = new VendVendTable_DirPartyTable_CompanyInfo();
            vendTable.createDirPartyTable().add(dirPartyTable);
            dirPartyTable.parmLanguageId(conPeek(rec, 10));
            //companyInfo.parmTCC


            organizationName = dirPartyTable.createOrganizationName().addNew();
            organizationName.parmName(conPeek(rec, 3));

            adresss = dirPartyTable.createDirPartyPostalAddressView().addNew();
            adresss.parmCity(conPeek(rec, 5));
            adresss.parmCountryRegionId(conPeek(rec, 6));
            adresss.parmStreet(conPeek(rec, 7));
            adresss.parmZipCode(conPeek(rec, 8));
            adresss.parmRoles('Business');

            contactInfo = dirPartyTable.createDirPartyContactInfoView().addNew();
            contactInfo.parmIsPrimary(NoYes::Yes);
            contactInfo.parmType(LogisticsElectronicAddressMethodType::Phone);
            contactInfo.parmRoles('Business');
            contactInfo.parmLocator(conPeek(rec, 9));

            vendVendTableService.create(vendVendTable);
        }

        ttsCommit;
    }

}

11 comments:

  1. Thanks for you code...that really helpful...what if i want to create or update address for existing vendor in vendor master...please share some ideas....

    ReplyDelete
    Replies
    1. Hi Asraf,
      First you have to Find or Read using service which you have to pass the EntityKeylist for read and QueryList for Find it will return the service axdVendTableservice class...
      and you have to update the new values in that object and you have to call update method of your service.

      for find or read reference you can see the post
      http://krishhdax.blogspot.dk/2011/11/call-file-outbound-aif-service-using.html

      This will helpful for you to write some sample to read or find.

      Delete
    2. In your code.. you are giving vendor account as defined...what if i want to use it as numbersequence...In above method i used numberseq class to get new numbersequence and given the value to (vendTable.parmAccountNum(numberseq::numgetnew());)...it returning the next numbersequence but...when i run this class the ax wont accept this vendor account rather giving the error (The number sequence AP_001 does not allow the Vendor account to be defined.) waiting for your feed back

      Delete
    3. Hey all,

      I am sending out a note to those of you that have taken advantage of my Blog(http://krishhdax.blogspot.com),through skype,Chat..etc, that I have been making over the past couple of years, asking for a favor.

      It is that time of year 2013, and we are now announcing the nomination process for The Most Influential People in Microsoft Dynamics for 2013. I would really like to make the list this year, and I hope that I have influenced you all in my own little way.

      If you feel inclined, you can nominate me by sending an email with a persuasive explanation for the nomination to top100@dynamicsworld.co.uk or through Twitter using the #dwtop100 tag.

      You can find more on the nomination process can be found here: http://www.dynamicsworld.co.uk/top100nominationprocess/

      Thanks in advance.

      Krishna Reddy Modugula | CGI |AX Technical Manager | Microsoft Solutions
      Lautrupvang 10, DK-2750 Ballerup | Denmark
      Phone: +45 44 78 40 00 | Mobile: +45 41 88 23 35
      www.cgi.com | www.cgi.dk
      http://krishhdax.blogspot.com
      http://www.linkedin.com/in/krishhprofile

      Delete
  2. Hi Asaraf,
    Thanks a lot for your example code, I've used this as a template for importing customers.
    There's one issue, though, that I'm having some problems with.
    If youe have more tha one contactInfo (like phone and email) you need to create more thant one record in the DirPartyPostalAddressView. I've tried to do this in many ways, but I only get an error "Data item DirPartyContactInfoView already exists."
    Do you have a solution to that?

    best regards,
    Ola

    ReplyDelete
    Replies
    1. Hi Ola,
      I have added the new post regarding your functionality.

      http://krishhdax.blogspot.dk/2012/07/ax2012-import-vendors-from-csv-using.html

      Hope this post will help you.

      Regards,
      krishna.

      Delete
    2. Hi Krishna,
      Thanks a lot for your quick respones, you've saved my day!
      I'm not sure, but I think what did the trick was the line where you added the code "aifEntityKeyList = vendVendTableService.create(vendVendTable);
      " and used that instead of the "vendVendTableService.create(vendVendTable);".

      regards,
      Ola

      Delete
  3. Hi my name is alex, your code is excellent, but I have a problem, I'm from Mexico and I have a field named vendortype_MX but there is no a parm method for it, and ax says "this field is required". U know how I can solve that?? in advance, thanks.

    ReplyDelete
    Replies
    1. Hi Alex,
      You have to create the parmMethod for the vendorType_MX field in the AXVendVendTable class.

      other wise update the service vendVendTableService from AiFserviceWizard.

      Regards,
      krishna.

      Delete
    2. Hey all,

      I am sending out a note to those of you that have taken advantage of my Blog(http://krishhdax.blogspot.com),through skype,Chat..etc, that I have been making over the past couple of years, asking for a favor.

      It is that time of year 2013, and we are now announcing the nomination process for The Most Influential People in Microsoft Dynamics for 2013. I would really like to make the list this year, and I hope that I have influenced you all in my own little way.

      If you feel inclined, you can nominate me by sending an email with a persuasive explanation for the nomination to top100@dynamicsworld.co.uk or through Twitter using the #dwtop100 tag.

      You can find more on the nomination process can be found here: http://www.dynamicsworld.co.uk/top100nominationprocess/

      Thanks in advance.

      Krishna Reddy Modugula | CGI |AX Technical Manager | Microsoft Solutions
      Lautrupvang 10, DK-2750 Ballerup | Denmark
      Phone: +45 44 78 40 00 | Mobile: +45 41 88 23 35
      www.cgi.com | www.cgi.dk
      http://krishhdax.blogspot.com
      http://www.linkedin.com/in/krishhprofile

      Delete

Thanks for visiting my blog,
I will reply for your comment within 48 hours.

Thanks,
krishna.