Tuesday 29 March 2011

C# Updating/Populating Fields on CRM 2011 Plugin

The following code shows a plugin that retrieves a mobile number from a contact/account/lead from the 'To' field on an activity(custom or default) and also how to set a field on the form. Rremember you should register this plugin on PRE-OPERATION and as SYNCHRONOUS... theres no need to use images either here :) Note the code format will come out right when you copy and paste into an IDE!




public void Execute(IServiceProvider serviceProvider)

  {



   // Obtain the execution context from the service provider.

   IPluginExecutionContext context = (IPluginExecutionContext)

   serviceProvider.GetService(typeof(IPluginExecutionContext));





   // The InputParameters collection contains all the data passed in the message request.

   if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

   {

    // Obtain the target entity from the input parmameters.

    Entity entity = (Entity)context.InputParameters["Target"];



    try

    {

     if (entity.LogicalName.Equals("new_sms")) // Check if its the custom sms entity

     {





      IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

      IOrganizationService service = factory.CreateOrganizationService(context.UserId);



      if (entity["to"] != null)

      {       

       string ent = ((EntityReference)((EntityCollection)entity["to"]).Entities[0].Attributes["partyid"]).LogicalName;

       Guid id = ((EntityReference)((EntityCollection)entity["to"]).Entities[0].Attributes["partyid"]).Id;

       string field = "";

       // field = ent.Equals("account") ? "telephone1" : "mobilephone"; //This statement is elaborated below laborated below

       if (ent.Equals("account"))

       {

        field = "telephone1";

       }

       else if (ent.Equals("contact") || ent.Equals("lead"))

       {

        field = "mobilephone";

       }



       Entity var = service.Retrieve(ent, id, new ColumnSet(true));



       if (var.Attributes.Contains(field))

       {        

        string number = ((string)var[field]);

        

        if (entity.Attributes.Contains("new_txtmessage"))

        {

         entity.Attributes["new_txtmessage"] += "TEST MESSAGE";

        }

        else

        {

         entity.Attributes.Add("new_txtmessage", "Another message");

        }

       }



      }



     } ////

    }



    catch (InvalidPluginExecutionException e)

    {



     throw new InvalidPluginExecutionException("Error in Quick SMS plugin: " + e.Message);

    }



   }

  }

3 comments:

  1. Could you send me the updated code for pulling a contact field named new_callnumber after a phonecall is created?

    I'm not quite sure what needs to be changed and a real world example would help me tons!!!

    ReplyDelete
    Replies
    1. It would then update a field of the same name on the phonecall record.
      Pull new_callnumber from Contact on a phone call using the TO field to determine the contact and then retrieving a field named new_callnumber from Contact.
      this field is a global optionset(picklist)

      THANK you in advance!!!!

      Delete
    2. hi there,there isnt much to be changed at all!All you would need to do is check that its the phone call entity e.g:
      if (entity.LogicalName.Equals("phonecall")

      and the 'field' variable in this case would be 'new_callnumber'
      Given its a optionset,you would have to cast it to get the value. If its the label/text on the optionset you want, there are some wrapper classes out there already that can retrieve the text on an optionset.I wrote this code during my primitive days and I can see some areas for improvement,for example, I wouldnt retrieve ALL the attributes for the record,only need the id & the field needed.

      Delete