Showing posts with label plugin. Show all posts
Showing posts with label plugin. Show all posts

Sunday, 22 July 2012

CRM 4 Plugin Names 'blank' after upgrade

Ive found that on a number of occasions when doing an upgrade from CRM 4 -> CRM 2011 (through db attach) that the plugin names/steps appear blank when looking through the plugin registration tool.
A simple UNSUPPORTED sql script can fix this for you. Run this on the crm database (XXXX_mscrm) you want to patch up:

UPDATE PluginTypeBase 
SET Name = TypeName 
WHERE Name IS NULL




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

    }



   }

  }