Update in pre update plugin in CRM

In previous version i.e. 3.0  on updating certain field in pre/post update used to lead to circular reference.

The only way to update in case of crm 3.0 was to modify the entityxml recieived as ref parameter for preUpdate event handler.

In CRM 4.0 to do the same we can add/update the properties passed as inputparameters to the context for pre-update.

public void Execute(IPluginExecutionContext context)
{
DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties[“Target”];
if (entity.Name == EntityName.lead.ToString())
{

// firstname and lastname are already properties of the entity which are not null and

// therefore are passed as inputparamters

String firstName = “Nishant”;
entity.Properties[“firstname”] = firstName;

String lastName = “Rana”;
entity.Properties[“lastname”] = lastName;

//or setting a value of a field whose value is null i.e. not passed as inputparameter
StringProperty subject = new StringProperty(“subject”, “Test Subject”);
entity.Properties.Add(subject);

}
}

The above plugin is registered for lead’s pre-update event.

Using service.update would lead to “server is unable to process the request error” as it automatically checks for the circular reference, if the crmService has been created using context.CreateCrmService() method.

For CRM 3.0 refer this wonderful article

http://www.stunnware.com/crm2/topic.aspx?id=Callout2

Bye…


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s