Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CRM 2011: How to Update record in a Create Plugin?

I'm having serious problems in how to update the record that has just been created with some additional data.

Case: I have an sms activity. On create an sms record. A plugin fires to actualy send the sms. A third party sms provider takes cares of the sending and returns a status string. Based on these string, a status for the sms must be updated.

Here's some of my code:

public void Execute(IServiceProvider serviceProvider)
{
  IPluginExecutionContext context = (IPluginExecutionContext)
  serviceProvider.GetService(typeof(IPluginExecutionContext));

  IOrganizationServiceFactory serviceFactory = 
    (IOrganizationServiceFactory)serviceProvider.GetService(
      typeof(IOrganizationServiceFactory));
  IOrganizationService service = 
    serviceFactory.CreateOrganizationService(context.UserId);
  aContext orgContext = new aContext(service);

  Entity sms = (Entity)context.InputParameters["Target"];
  /// logic goes here

  sms.StatusCode = new OptionSetValue(statuscode); //statuscode is integer
  service.Update(sms);
}

I got a error in the plugin everytime i execute the plugin. Can someone help and explain me what i'm doing wrong here?

Thanks!

like image 426
ThdK Avatar asked Nov 17 '11 13:11

ThdK


People also ask

How do you update a record in Dynamics CRM?

Lookup: Select the record from the list of records to select the new lookup value. Create new record: Click on 'New' button to add a new row in a table. Enter data for all the required fields. Once you are done with making changes in the excel sheet click on publish to save the changes in the Dynamics 365 CRM.

How do I update my Dynamics 365 Plugin?

Updating the Plugin Next, you need to go to the Plugin Registration Tool, select the Plugin Assembly itself and click on Update. On this screen, even though you see your plugin already selected, click on the three dots to reselect the Assembly and select the .


1 Answers

As your plugin is executed synchronously, it should be easy to update your entity if you use the Pre-operation stage of execution.

In this case, you can just do something like this:

Entity sms = (Entity)context.InputParameters["Target"]

// additional code to retrieve status

if(sms.Attributes.Contains("statuscode"))
    sms.Attributes["statuscode"] = new OptionSetValue(statuscode);
else
    sms.Attributes.add("statuscode", new OptionSetValue(statuscode));

In that way, you'll just add or update a key from the Attributes dictionnary, and it will be saved as if the value was coming directly from the form.

like image 52
Renaud Dumont Avatar answered Nov 02 '22 22:11

Renaud Dumont