Wednesday 8 June 2011

c# plugin update error The product and the unit cannot be updated while the entity is locked.

It appears that if you have an order(sales order) that has its prices locked (done by clicking ‘Lock Pricing’ or converting the order to an invoice), you CANT update any field on it through a plugin. You can through the form though, but with the plugin you get an error message:

The product and the unit cannot be updated while the entity is locked.

The solution here would be to remove the references to those fields before doing the update. e.g.


bool check = (bool)job.Attributes["salesorderispricelocked"];
                                    if (check)
                                    {
                                        if (job.Attributes.Contains("productid"))
                                        {
                                            job.Attributes.Remove("productid");
                                        }
                                        if (job.Attributes.Contains("uomid"))
                                        {
                                            job.Attributes.Remove("uomid");
                                        }
                                        if (job.Attributes.Contains("priceperunit_base"))
                                        {
                                            job.Attributes.Remove("priceperunit_base");
                                        }
                                        if (job.Attributes.Contains("priceperunit"))
                                        {
                                            job.Attributes.Remove("priceperunit");
                                        }
                                        service.Update(job);
                                    }
                                    else
                                    {
                                        service.Update(job);
                                    }


This would be the same for the invoice entity too

Wednesday 4 May 2011

CRM 2011 System.NullReferenceException error occurs while trying to save an entity

I had the situation where by i kept getting an error whilst trying to save an entity. This error message file typically contained some of the following:

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: System.NullReferenceException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #2DBD7965Detail:

-2147220970

System.NullReferenceException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #2DBD7965
2011-03-04T08:30:14.1912773Z






******************************************************************************


I think this has to do with the default Microsoft.Crm.Extensibility plugins thats added to each org...it seems inside there, it references some of the steps you create in your custom plugin/workflow such that when you delete/remove your plugin, its still got traces to it and then you cant save records. i found this out by trying to delete my custom entity( which failed), so i had to deactivate the Microsoft.Crm.Extensibility steps first,then i could delete my entity.

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