How to create a customer record from within AX. (in a job or in a class method for eg)
Following are all the variables you need to declare. CustCustomerService is the service reference and you create an instance of it using the static create method.
//CustCustomer, CustCustomer_CustTable, CustCustomer_DirParty_DirOrganization all are data object classes, which together represent the XML you might pass into the service method (Create) to create a customer record.
CustCustomerService custService;
CustCustomer cust;
CustCustomer_CustTable custTable;
CustCustomer_DirParty dirParty;
CustCustomer_DirParty_DirOrganization dirOrg;
//Obtain an instance of the service
custService = CustCustomerService::construct();
//Customer record we need to create and pass to the above custService’s Create method
cust = new CustCustomer();
//CustCustomer has an array of CustCustomer_CustTable records, which allows us to create a number of customers in a single call. createCustTable().addNew() creates a CustCustomer_CustTable record adds it to CustCustomer and returns it for our use.
custTable = cust.createCustTable().addNew();
custTable.parmCustGroup(“10″);
custTable.parmTaxGroup(“CA”);
//CustCustomer_CustTable holds a collection of DirParty records, however we add entries of type CustCustomer_DirParty_DirOrganization to this variable. I am not yet sure why, please post a comment if you know why or i will update this when i figure it out. Edit: We can because a customer is just a role and eventually all roles are linked to Parties. A party can be a person or organization.
dirOrg = new CustCustomer_DirParty_DirOrganization();
dirOrg.parmName(“MyCustomerName_OrgName”);
dirOrg.parmLanguageId(“en-us”);
custTable.createDirParty().add(dirOrg);
//Finally! we create the customer record
custService.create(cust);
Following MSDN article explains how to do the same as above but for SalesOrder by passing in an XML to the create method
http://msdn.microsoft.com/en-us/library/cc598792.aspx
The following article is an example in C# for doing the same
http://shashidotnet.wordpress.com/2012/01/06/ax-2012-aif-calling-the-custcustomerservice-create-method-from-wcf/