Microsoft Dynamics CRM 2011

Microsoft Dynamics CRM 2011

Tuesday, April 21, 2015

How to use Tracing in CRM 2011-2013 Plugins and Workflows

In this article we describe Step by step How to use Tracing in CRM 2011-2013 Plugins and Workflows in C# in 5 minutes.
Since CRM Plugins and Workflows are deployed on web servers after the development reaches the release stage , it is usually very helpful to include tracing in your code in profusion, not only in error cases, at least when you are putting to the test new developments .
CRM 2011-2013 allows you to include tracing galore:
How to use Tracing in CRM 2011-2013 Plugins and Workflows



How to use Tracing in CRM 2011-2013 Plugins and Workflows



Phase 1: use the GetExtension() method from the "context" at your plugin-workflow:
Tracing in CRM 2011-2013 Plugins and Workflows


Since this is a generic C# Dot.Net method, use the ITracingService interface to get the tracing service from the context.


Phase 2: Send the ITracingService to any method where you intend to log information using tracing:

CRM 2011-2013 Plugins and Workflows

For example:

How to use Tracing in CRM


Phase 3: inside the plugin's methods, call the ITracingService's Trace() method to send messages to the tracing:

How to use Tracing in CRM 2011-2013


In case of error, CRM will display an alert showing the tracing:


How to use Tracing in CRM 2011-2013 Plugins and Workflows


That's all...Enjoy Dynamics CRM

by Carmel Schvartzman

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


    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

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