Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Unanswered

X++ code for creating and updating customers with address in ax 2009

Posted on by 184

Hi All,

I have a requirement to create and update customers such as dob,address etc.

Can I get some x++ code for this please? 

Or any blog or link which has the code also fine please.  

Please send me ax 2009 based code

  • D365_Wibes Profile Picture
    D365_Wibes 184 on at
    RE: X++ code for creating and updating customers with address in ax 2009

    Thanks Martin.. Let me try this and come back to you ,,,,,

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: X++ code for creating and updating customers with address in ax 2009

    Let me try to do the simplication for you. Remember, the currect question is about updating an address, not about all the other fields. Also, let me eliminate the second service class object.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using CustomerServiceReference;
    
    class Program
    {
    	static void Main(string[] args)
    	{
    		new Program().Run();
    	}
    	
    	private void Run()
    	{
    		string accountNum = "Cust0001";
    		string newAddress = "the new address";
    		
    		EntityKey[] entityKey = GetEntityKey(accountNum)
    		using (CustomerServiceClient proxy = CreateServiceClient())
    		{
    			AxdCustomer customer = proxy.read(entityKey);
    			AxdEntity_CustTable custTable = customer.CustTable[0];
    			custTable.Address = newAddress;
    			
    			proxy.update(entityKey, customer);
    		}
    		
    	}
    
    	private CustomerServiceClient CreateServiceClient()
    	{
    		CustomerServiceClient proxy = new CustomerServiceClient();
    		
    		proxy.ClientCredentials.Windows.AllowNtlm = true;
    		proxy.ClientCredentials.UserName.UserName = ""; // Removed for security reasons
    		proxy.ClientCredentials.UserName.Password = ""; // Removed for security reasons
    		
    		return proxy;			
    	}
    
    	private EntityKey[] GetEntityKey(string accountNum)
    	{
    		KeyField field = new KeyField()
    		{
    			Field = "AccountNum",
    			Value = accountNum
    		};
    		
    		EntityKey key = new EntityKey()
    		{
    			KeyData = new[] { field }
    		};
    	 
    		return new[] { key };		
    	}
    }

    What happens if you run this code? Of course, don't forget to set input values. And sorry if there are some errors; I wrote the code in a mere text editor.

  • D365_Wibes Profile Picture
    D365_Wibes 184 on at
    RE: X++ code for creating and updating customers with address in ax 2009

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace UCQCustCustomerService
    {
        class Program
        {
            private static void updateCustomer(CustomerServiceReference.AxdCustomer customer)
            {
                
                CustomerServiceReference.AxdCustomer custRec;
                
                CustomerServiceReference.CustomerServiceClient updateCust = new CustomerServiceReference.CustomerServiceClient();
                updateCust.ClientCredentials.Windows.AllowNtlm = true;
                updateCust.ClientCredentials.UserName.UserName = "";  // Removed for security reasons
                updateCust.ClientCredentials.UserName.Password = ""; // Removed for security reasons
                CustomerServiceReference.AxdEntity_CustTable custTable = customer.CustTable[0];
                CustomerServiceReference.AxdEntity_CustTable custUpdate = new CustomerServiceReference.AxdEntity_CustTable();
                CustomerServiceReference.AxdCustomer axCustomer = new CustomerServiceReference.AxdCustomer();
    
                custUpdate.action = CustomerServiceReference.AxdEnum_AxdEntityAction.update;
    
                custUpdate.AccountNum = customer.CustTable[0].AccountNum;
                
                custUpdate.Address = customer.CusTable[0].Address;
               
                custUpdate.CustClassificationId = customer.CustTable[0].CustClassificationId;
                
                custUpdate.CustItemGroupId = customer.CustTable[0].CustItemGroupId;
                custUpdate.CustGroup = customer.CustTable[0].CustGroup;
                custUpdate._DocumentHash = customer.CustTable[0]._DocumentHash;
                custUpdate.LanguageId = customer.CustTable[0].LanguageId;
                custUpdate.Currency = customer.CustTable[0].Currency;
                custUpdate.PartyType = customer.CustTable[0].PartyType;
                custUpdate.TaxGroup = customer.CustTable[0].TaxGroup;
                axCustomer.CustTable = new CustomerServiceReference.AxdEntity_CustTable[1] { custUpdate };
    
                CustomerServiceReference.EntityKey[] readCustKeys = new CustomerServiceReference.EntityKey[1];
                readCustKeys[0] = new CustomerServiceReference.EntityKey();
                readCustKeys[0].KeyData = new CustomerServiceReference.KeyField[1];
                readCustKeys[0].KeyData[0] = new CustomerServiceReference.KeyField();
                readCustKeys[0].KeyData[0].Field = "AccountNum";
                readCustKeys[0].KeyData[0].Value = custUpdate.AccountNum;
                
                
    
                updateCust.update(readCustKeys, axCustomer);
                //CustomerServiceReference.AxdEntity_CustTable[1] { custUpdate };
                // Create the entity key list for the request
    
                
    
               // readCustKeys[0].KeyData[0].Value = custUpdate.Address;
    
               // custRec = updateCust.read(readCustKeys);
    
                Console.WriteLine("Updated Address is : "   custUpdate.Address);
                Console.WriteLine("*********************************************");
    
            }
    
            enum AXGender
            {
                Blank,
                Male,
                Female,
                NotStated
            }
    
            static void Main(string[] args)
            {
                string male,female;
               
    
               
    
                CustomerServiceReference.CustomerServiceClient proxy = new CustomerServiceReference.CustomerServiceClient();
                CustomerServiceReference.AxdCustomer custRec;
                
                proxy.ClientCredentials.Windows.AllowNtlm = true;
                proxy.ClientCredentials.UserName.UserName = "";
                proxy.ClientCredentials.UserName.Password = "";
                
                // Create the entity key list for the request
    
                CustomerServiceReference.EntityKey[] readCustKeys = new CustomerServiceReference.EntityKey[1];
                readCustKeys[0] = new CustomerServiceReference.EntityKey();
                readCustKeys[0].KeyData = new CustomerServiceReference.KeyField[1];
                readCustKeys[0].KeyData[0] = new CustomerServiceReference.KeyField();
                readCustKeys[0].KeyData[0].Field = "AccountNum";
               
    
                // Promtp User for Cust Acc
    
                Console.WriteLine("Provide Customer Acc Number :");
    
                // Add the result to the entity key value.
    
                readCustKeys[0].KeyData[0].Value = Console.ReadLine();
    
                
    
                try
                {
                    custRec = proxy.read(readCustKeys);
                    
                    // Display customer info before Change
    
                    Console.WriteLine("For Customer: "   custRec.CustTable[0].AccountNum);
                    Console.WriteLine("DataAreaId  is: "   custRec.CustTable[0].DataArea);
                    Console.WriteLine("Current Address is: "   custRec.CustTable[0].Address);
    
                    // After Customer Update
    
                    Console.WriteLine("Provide Customer Acc Number to update :");
    
                    // Add the result to the entity key value.
                    
                    readCustKeys[0].KeyData[0].Value = Console.ReadLine();
                    
                    Console.WriteLine("Update Address :");
                    
                    //readCustKeys[0].KeyData[0].Value = Console.ReadLine();
                    
                    custRec = proxy.read(readCustKeys);
    
                    custRec.CustTable[0].Address = Console.ReadLine();
                    
                    Console.WriteLine("Customer Group Id :");
                    custRec.CustTable[0].CustItemGroupId = Console.ReadLine();
                    Console.WriteLine("Customer Group :");
                    custRec.CustTable[0].CustGroup = Console.ReadLine();
                    updateCustomer(custRec);
    
    
    
                    Console.ReadKey();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: "   e.Message);
                    Console.ReadKey();
                    proxy.Abort();
                }
                //proxy.Close();
            }
        }
    }

    I have pasted by removing code which is not required. And now i need all this code. Please can suggest me how to pass address to customer from Console.Readline()

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: X++ code for creating and updating customers with address in ax 2009

    Could you please remove the code that isn't relavant to the question, the variable that aren't used at all and so on? It'll be then much easier to focus on the actual problem.

  • D365_Wibes Profile Picture
    D365_Wibes 184 on at
    RE: X++ code for creating and updating customers with address in ax 2009

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace UCQCustCustomerService
    {
        class Program
        {
            private static void updateCustomer(CustomerServiceReference.AxdCustomer customer)
            {
                
                CustomerServiceReference.AxdCustomer custRec;
                
                CustomerServiceReference.CustomerServiceClient updateCust = new CustomerServiceReference.CustomerServiceClient();
                updateCust.ClientCredentials.Windows.AllowNtlm = true;
                updateCust.ClientCredentials.UserName.UserName = "";  // Removed for security reasons
                updateCust.ClientCredentials.UserName.Password = ""; // Removed for security reasons
                CustomerServiceReference.AxdEntity_CustTable custTable = customer.CustTable[0];
                CustomerServiceReference.AxdEntity_CustTable custUpdate = new CustomerServiceReference.AxdEntity_CustTable();
                CustomerServiceReference.AxdCustomer axCustomer = new CustomerServiceReference.AxdCustomer();
    
                custUpdate.action = CustomerServiceReference.AxdEnum_AxdEntityAction.update;
    
                custUpdate.AccountNum = customer.CustTable[0].AccountNum;
                
                custUpdate.Address = customer.CusTable[0].Address;
               
                custUpdate.CustClassificationId = customer.CustTable[0].CustClassificationId;
                
                custUpdate.CustItemGroupId = customer.CustTable[0].CustItemGroupId;
                custUpdate.CustGroup = customer.CustTable[0].CustGroup;
                custUpdate._DocumentHash = customer.CustTable[0]._DocumentHash;
                custUpdate.LanguageId = customer.CustTable[0].LanguageId;
                custUpdate.Currency = customer.CustTable[0].Currency;
                custUpdate.PartyType = customer.CustTable[0].PartyType;
                custUpdate.TaxGroup = customer.CustTable[0].TaxGroup;
                axCustomer.CustTable = new CustomerServiceReference.AxdEntity_CustTable[1] { custUpdate };
    
                CustomerServiceReference.EntityKey[] readCustKeys = new CustomerServiceReference.EntityKey[1];
                readCustKeys[0] = new CustomerServiceReference.EntityKey();
                readCustKeys[0].KeyData = new CustomerServiceReference.KeyField[1];
                readCustKeys[0].KeyData[0] = new CustomerServiceReference.KeyField();
                readCustKeys[0].KeyData[0].Field = "AccountNum";
                readCustKeys[0].KeyData[0].Value = custUpdate.AccountNum;
                
                
    
                updateCust.update(readCustKeys, axCustomer);
                //CustomerServiceReference.AxdEntity_CustTable[1] { custUpdate };
                // Create the entity key list for the request
    
                
    
               // readCustKeys[0].KeyData[0].Value = custUpdate.Address;
    
               // custRec = updateCust.read(readCustKeys);
    
                Console.WriteLine("Updated Address is : "   custUpdate.Address);
                Console.WriteLine("*********************************************");
    
            }
    
            enum AXGender
            {
                Blank,
                Male,
                Female,
                NotStated
            }
    
            static void Main(string[] args)
            {
                string male,female;
               
    
               
    
                CustomerServiceReference.CustomerServiceClient proxy = new CustomerServiceReference.CustomerServiceClient();
                CustomerServiceReference.AxdCustomer custRec;
                
                proxy.ClientCredentials.Windows.AllowNtlm = true;
                proxy.ClientCredentials.UserName.UserName = "";
                proxy.ClientCredentials.UserName.Password = "";
                
                // Create the entity key list for the request
    
                CustomerServiceReference.EntityKey[] readCustKeys = new CustomerServiceReference.EntityKey[1];
                readCustKeys[0] = new CustomerServiceReference.EntityKey();
                readCustKeys[0].KeyData = new CustomerServiceReference.KeyField[1];
                readCustKeys[0].KeyData[0] = new CustomerServiceReference.KeyField();
                readCustKeys[0].KeyData[0].Field = "AccountNum";
               
    
                // Promtp User for Cust Acc
    
                Console.WriteLine("Provide Customer Acc Number :");
    
                // Add the result to the entity key value.
    
                readCustKeys[0].KeyData[0].Value = Console.ReadLine();
    
                
    
                try
                {
                    custRec = proxy.read(readCustKeys);
                    
                    // Display customer info before Change
    
                    Console.WriteLine("For Customer: "   custRec.CustTable[0].AccountNum);
                    Console.WriteLine("DataAreaId  is: "   custRec.CustTable[0].DataArea);
                    Console.WriteLine("Current Address is: "   custRec.CustTable[0].Address);
    
                    // After Customer Update
    
                    Console.WriteLine("Provide Customer Acc Number to update :");
    
                    // Add the result to the entity key value.
                    
                    readCustKeys[0].KeyData[0].Value = Console.ReadLine();
                    
                    Console.WriteLine("Update Address :");
                    
                    //readCustKeys[0].KeyData[0].Value = Console.ReadLine();
                    
                    custRec = proxy.read(readCustKeys);
    
                    custRec.CustTable[0].Address = Console.ReadLine();
                    
                    Console.WriteLine("Customer Group Id :");
                    custRec.CustTable[0].CustItemGroupId = Console.ReadLine();
                    Console.WriteLine("Customer Group :");
                    custRec.CustTable[0].CustGroup = Console.ReadLine();
                    updateCustomer(custRec);
    
    
    
                    Console.ReadKey();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: "   e.Message);
                    Console.ReadKey();
                    proxy.Abort();
                }
                //proxy.Close();
            }
        }
    }

    Please find whole console C# code as below:

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: X++ code for creating and updating customers with address in ax 2009

    I don't see anything like that in your code. Please add the relevant pieces to your code, remove the irrelevant ones (such as the other fields) and share your code with us.

  • D365_Wibes Profile Picture
    D365_Wibes 184 on at
    RE: X++ code for creating and updating customers with address in ax 2009

    No Martin, actually before assigning the address I am taking new address as input via Console.Readline() and then pass that to (customer) and then finally I assign it as below

    custUpdate.Address = customer.CustTable[0].Address;

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: X++ code for creating and updating customers with address in ax 2009

    What change do you expect? It seems to me that you're assigning the address that the customer already has, therefore there is nothing to change.

  • D365_Wibes Profile Picture
    D365_Wibes 184 on at
    RE: X++ code for creating and updating customers with address in ax 2009

    CustomerServiceReference.AxdEntity_CustTable custTable = customer.CustTable[0];
    CustomerServiceReference.AxdEntity_CustTable custUpdate = new CustomerServiceReference.AxdEntity_CustTable();
    CustomerServiceReference.AxdCustomer axCustomer = new CustomerServiceReference.AxdCustomer();
    
    custUpdate.action = CustomerServiceReference.AxdEnum_AxdEntityAction.update;
    
    custUpdate.AccountNum = customer.CustTable[0].AccountNum;
    custUpdate.PPTAusPostBillPayCode = customer.CustTable[0].PPTAusPostBillPayCode;
    custUpdate.CustClassificationId = customer.CustTable[0].CustClassificationId;
    custUpdate.Blocked = customer.CustTable[0].Blocked;
    custUpdate.CellularPhone = customer.CustTable[0].CellularPhone;
    custUpdate.ECL_ACMDateOfDeath = customer.CustTable[0].ECL_ACMDateOfDeath;
    custUpdate.ECL_ACMDVAPensionNo = customer.CustTable[0].ECL_ACMDVAPensionNo;
    custUpdate.ECL_ACMFirstName = customer.CustTable[0].ECL_ACMFirstName;
    custUpdate.ECL_ACMGender = customer.CustTable[0].ECL_ACMGender;
    custUpdate.CustItemGroupId = customer.CustTable[0].CustItemGroupId;
    custUpdate.CustGroup = customer.CustTable[0].CustGroup;
    custUpdate._DocumentHash = customer.CustTable[0]._DocumentHash;
    custUpdate.LanguageId = customer.CustTable[0].LanguageId;
    custUpdate.Currency = customer.CustTable[0].Currency;
    custUpdate.PartyType = customer.CustTable[0].PartyType;
    custUpdate.TaxGroup = customer.CustTable[0].TaxGroup;
    axCustomer.CustTable = new CustomerServiceReference.AxdEntity_CustTable[1] { custUpdate };
    
    CustomerServiceReference.EntityKey[] readCustKeys = new CustomerServiceReference.EntityKey[1];
    readCustKeys[0] = new CustomerServiceReference.EntityKey();
    readCustKeys[0].KeyData = new CustomerServiceReference.KeyField[1];
    readCustKeys[0].KeyData[0] = new CustomerServiceReference.KeyField();
    readCustKeys[0].KeyData[0].Field = "AccountNum";
    readCustKeys[0].KeyData[0].Value = custUpdate.AccountNum;

    I have pasted my C# Code.

    Its working fine for updating Customer cellular no and name etc.

    But when address as below it is not updating though I dont get any errors:

    custUpdate.Address = customer.CustTable[0.Address;

    Could you please let me know what am I doing wrong here?

    And I am taking address try Console.REadLine() and passing it 

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: X++ code for creating and updating customers with address in ax 2009

    Sorry, I don't have that particular piece of code for that particular version of AX.

    But I (and others) could help if you explain your particular problem.

    You said that you have a problem with the address - does it mean that you know how to update other information, such as customer name?

    What exactly are struggling with?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans