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);

    }



   }

  }

Sunday 27 March 2011

Getting Web Resources from the CRM 2011 Ribbon

If your creating a custom action/ribbon feature and you want to reference a file in your webresources, then adding the $websresource variable will allow for this.
e.g and image location could be


<Button
Id="B_TextButton"
LabelText="Send TXT"
ToolTipTitle="Send a TXT Message"
ToolTipDescription="Send a TXT message to this entity record, if the mobile number is provided"
TemplateAlias="o1"
Image16by16= "$webresource:new_txt16.png"
Image32by32="$webresource:new_txt32.png"/>


</CommandUIDefinition>

How to add a custom button in CRM 2011

Usefuly blog link on how to do this:
LINK

New CRM 2011 Javascript for fields

This is a cool link which provides the basics of setting field values with the new Xrm javascript :

Link