locked
crm plugin :retrieve and update RRS feed

  • Question

  •     public class Class1 :IPlugin
        {   public void Execute(IServiceProvider serviceProvider)
            {     Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                    serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
                    try                {
                        if (context.Depth == 1)
                        {     IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                            Entity contact = new Entity("contact");

                            // Obtain the target entity from the input parmameters.
                            EntityReference entity = (EntityReference)context.InputParameters["Target"];
                            ColumnSet cols = new ColumnSet(new String[] { "lastname", "firstname", "address1_name" });

                            contact =service.Retrieve("contact", entity.Id, cols);

                            if (contact != null)
                            {
                                if (contact.Attributes.Contains("address1_name") == false)
                                {
                                    Random rndgen = new Random();
                                    contact.Attributes["address1_name"]= "first time value: " + rndgen.Next().ToString();
                                }
                                else
                                { contact["address1_name"] = "i already exist"; }
                                service.Update(contact);
                           }
                        }
                    }
                    catch (Exception e) { Console.WriteLine(e.Message); }    
            } }

    this is my plugin.

    i am trying to retrieve data from the contact and updating at the same time.In my registration i have given update to contact.

    i show me a exception.how to rectify it. 

    Monday, April 10, 2017 2:57 PM

Answers

  • Hi,

    Why are you converting the target input parameter to EntityReference if your plugin is registered on update of contact? It should be an Entity instead of EntityReference.

    Change this line 

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

    to

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
          Entity entity = (Entity)context.InputParameters["Target"];
    }
    

    Thanks


    Sachith Chandrasiri

    • Marked as answer by learner_test Tuesday, April 11, 2017 5:55 AM
    Monday, April 10, 2017 11:35 PM

All replies

  • Hi,

    Why are you converting the target input parameter to EntityReference if your plugin is registered on update of contact? It should be an Entity instead of EntityReference.

    Change this line 

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

    to

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
          Entity entity = (Entity)context.InputParameters["Target"];
    }
    

    Thanks


    Sachith Chandrasiri

    • Marked as answer by learner_test Tuesday, April 11, 2017 5:55 AM
    Monday, April 10, 2017 11:35 PM
  • thanks for help,

    can u help with little insight on the difference between Entity and EntityReference.And when to use Entityreference


    Tuesday, April 11, 2017 5:55 AM