public class CalculDelaisDemande : IPlugin
public void Execute(IServiceProvider serviceProvider)
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = factory.CreateOrganizationService(context.UserId);
if (context.Depth != 1)
using (var orgContext = new Microsoft.Xrm.Sdk.Client.OrganizationServiceContext(service))
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
var entityDemande = (Entity) context.InputParameters["Target"];
var demande = (from c in orgContext.CreateQuery<new_demande>()
where c.new_demandeId == entityDemande.Id
A workflow is associate and should be launch on "record fields change".
But it is always launch even though I do not change the value of the field.
i think Organization context sends all the fields back to the server on update and not the changed ones. since you did call an update object on the record, it would have sent all values back to database and this caused the workflow to fire. HTH Sam
Can you allow the workflow to fire, but in the first step check a condition and stop as cancelled if the fields you are interested in have not changed?
(this would probably require storing the old value in another field to compare against, and updating the old one to match if the new one genuinely has changed, just like old-school CRM 4.0 workarounds to provide very limited "auditing" of changes)