I have opportunity, what I need is - when the est time changed the valid_to changed in the subgrid also changed to the same value. I have tried to write plugin to do that for me but nothing is happening, the product in the subgrid values still the same. What’s wrong?
I made plugin and this is the code:
public void Execute(IServiceProvider serviceProvider)
{
// extract the service provider
ITracingService tracingservice = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory srevicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = srevicefactory.CreateOrganizationService(context.UserId);
if(context.InputParameters.Contains("Target")&&context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.Contains("name"))
{
var fetch = @"<fetch no-lock='true' >
<entity name='opportunity' >
<attribute name='contactid'/>
<filter>
<condition attribute='opportunityid' operator='eq' value='{0}' />
</filter>
</entity>
</fetch>";
var fetchXML = string.Format(fetch, entity.Id);
var allContacts = service.RetrieveMultiple(new FetchExpression(fetchXML)).Entities;
foreach (var contactEnt in allContacts)
{
Entity contactToUpdate = new Entity("opportunityproduct", contactEnt.Id);
contactToUpdate["new_valid_to"] = entity["estimatedclosedate"];
service.Update(contactToUpdate);
}
}
}
}
I recommend you few things, for a beginner to learn.
tracingservice.Trace
to trace the code execution and debug the issueInvalidPluginExecutionException
for troubleshootingThis could be a simple copy/paste error. But the code is checking for attribute “name”, if there is no change made to name attribute in target entity, then this condition fails. Maybe it should be checking “estimatedclosedate” attribute if your plugin step filtering attribute is the same.
if (entity.Contains("estimatedclosedate")) //changed name into estimatedclosedate
{
tracingService.Trace("condition passed and est_date is in target entity.");
throw new InvalidPluginExecutionException("Debugging...");