Microsoft Dynamics CRM 2011

Microsoft Dynamics CRM 2011

Wednesday, April 15, 2015

How to programmatically create a CustomerAddress in Dynamics CRM 2011/2013


  1. In this article we see how to programmatically create a CustomerAddress in Dynamics CRM 2011/2013. 
    In CRM 2013, the address data of an Account or a Contact are stored in a separate Entity called CustomerAddress, on a 1:N relationship basis, in which an Account can have (and have, by default) many addresses.
    This post is about Dynamics CRM 2011 - 2013 custom Plugins or custom Workflows, and any   C# code which calls the CRM Organization Web Service to create a new CustomerAddress item , that means, a third address for an Account or a Contact. The following snapshot shows the Account address fields:
    How to programmatically create a CustomerAddress in Dynamics CRM 2011/2013

How to programmatically create a CustomerAddress in Dynamics CRM 2011/2013


  1. When an Account is created, CRM creates by default 2 addresses for it, from  the CustomerAddress entity type, which are called Address1 and Address2.
  2. If you intend to programmatically update the Account or Contact address, and not to create a new address, you just instantiate the following Account fields with the data, and CRM will automatically update the corresponding CustomerAddresses objects related to the specific Account:
oAccount["address1_telephone1"]
oAccount["address1_line1"]oAccount["address1_city"]oAccount["address1_postalcode"]


Change "Address1" with "Address2" if you want to update the second Account address.

If you intend to create a new (third) CustomerAddress item for the Account, follow this step by step instructions:

  1. First we write a check to see whether we got a legal ID of the Account we want to refer to:

    if (guidParentAccount.HasValue){
  2. Then we create the CustomerAddress entity , and instantiate the "ParentID" of the CustomerAddress . When creating a new address for an Account or a Contact entity, it's crucial to provide a reference field which points to the "ParentId" , that means , the Account or Contact to which belongs this new address. The C# code below instantiates the "ParentId" property with the actual Account , and not just the Parent's  ID (guid) :

    Entity oAccountAddress = new Entity("customeraddress");                                  
    EntityReference parentID = new EntityReference();                   
    parentID.Id = guidParentAccount.Value;                   
    parentID.LogicalName = "account";
                       
     oAccountAddress.Attributes["parentid"] = parentID;
  3. However, if you try to instantiate this "ParentId" field with the guid, you will be confronted with an exception , that's raised informing that the value is null : this error message is misleading, and we wrote about it in another article here.
  4. Another way, shorter, to instantiate the ParentID is the following:

    oAccountAddress["parentid"] = new EntityReference("account", guidParentAccount.Value );
  5. Next we save the street in the "line1" field: for the example, here i made a street search by street code on a generic List<> which contains street codes and descriptions:
                    

      string sStreetDescription = String.Empty;                   
      if ( oStreetList != null &&  oStreetList.Count > 0  && iStreetCode > 0)                   
      {                       
               sStreetDescription = oStreetList.FirstOrDefault(sc => sc.Code == iStreetCode).Description;                       
               if (sStreetDescription.Length > 0)                       
               {                           
                         oAccountAddress["line1"] = sStreetDescription;                       
               }                   
      }
  6. Next we save the city  in the "city" field: for this example, here i got a city object also from data migration, which contains both city code and description:

    string sCityDescription = String.Empty;                   
    if (oCity != null)                   
    {                       
          sCityDescription = oCity.Description;                       
          if (sCityDescription.Length > 0)                       
          {                           
                   oAccountAddress["city"] = sCityDescription;                       
          }                   
    }


  7. Then we instantiate the "postalcode" and "telephone1" properties:

     if (oAccount.Zip != null)                    
    {                        
             oAccountAddress["postalcode"] = Convert.ToString(oAccount.Zip);                    
    }
    oAccountAddress["telephone1"] = oAccount.Phone ?? "0";
  8. Finally we call the Organization Web Service to create the CustomerAddress entity from the Account:

    Guid? guidAccountAddress = XrmContext.Create(oAccountAddress);
  9. The CRM database will then reflect the 3 addresses (AddressNumber 1,2 and 3) as shown here:
    How to programmatically create a CustomerAddress in Dynamics CRM 2011/20131
     


That's all...Enjoy Dynamics CRM!!!

by Carmel Schvartzman

כתב: כרמל שוורצמן

    1 comment:

    1. Hello is there a way how to make newly inserted address primary address for account if it has already one ?

      ReplyDelete