CRM 2011: how to update a record in Create Plugin?

I am having serious problems updating the record that was just created with some additional data.

Case: I have sms activity. Create an sms entry. The plugin starts the actual sending of SMS. A third-party sms provider takes care of the sending and returns a status bar. Based on this line, you need to update the status for sms.

Here are some of my codes:

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 have a bug in the plugin every time I run the plugin. Can someone help and explain to me what I'm doing wrong here?

Thank!

+5
source share
1 answer

, , Pre-operation.

- :

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

, , , .

+12

All Articles