Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Unanswered

When a user changes the Sales Status to Bought for a Vehicle, close all related tasks of that Vehicle as Completed.

Posted on by Microsoft Employee

how do i fix this. Thanks everyone

{
public class Class1 : IPlugin
{
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);
ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
trace.Trace("optain the tracking service");
try
{
//obtain target entity from the inputparameter
Entity target = (Entity)context.InputParameters["Target"];
//retrive record
Entity entity = service.Retrieve("task", target.Id, new ColumnSet(true));

EntityReference regardingCaseRef = entity["regardingobjectid"] as EntityReference;
//retrive regarding from the back end
trace.Trace("2");
Entity regardingCase = service.Retrieve(regardingCaseRef.LogicalName, regardingCaseRef.Id, new ColumnSet(true));
//retrieve entityrecord from the ms crm server that match the
trace.Trace("4");
QueryExpression qeRelatedTasks = new QueryExpression("task");
qeRelatedTasks.ColumnSet = new ColumnSet(true);
qeRelatedTasks.Criteria.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Equal, regardingCaseRef.Id));
EntityCollection relatedTasks = service.RetrieveMultiple(qeRelatedTasks);


if (AllTaskReject(relatedTasks))
{
trace.Trace("status check");
regardingCase["statuscode"] = new OptionSetValue(912450000);
regardingCase["cre7a_bigfour"] = new OptionSetValue(1);

regardingCase["description"] = "All task update csutaskstatus";
trace.Trace("status check 2");

service.Update(regardingCase);
//update the case status == task status after check if
}

}
catch
{
throw;

}
}
public bool AllTaskReject(EntityCollection relatedTasks)
{
//check all the the task if rejected continue if on task not rejected do nothing

if (relatedTasks.Entities.All(x => x.GetAttributeValue<OptionSetValue>("cos_csutaskstatus")?.Value == 769190000))
{
return true;

}
else
{
return false;
}

}
}

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: When a user changes the Sales Status to Bought for a Vehicle, close all related tasks of that Vehicle as Completed.

       try

               {

                   trace.Trace("inside try");

                   //obtain the target entity from the inputparameter

                   Entity account = (Entity)context.InputParameters["Target"];

                   //rettrive

                   trace.Trace("retrieve task");

                   Entity entity = service.Retrieve("account", account.Id, new ColumnSet("cre7a_salesstatus"));

                   EntityReference regardingCaseRef = entity["regardingobjectid"] as EntityReference;

                   //retrive regarding from the back end

                   trace.Trace("2");

                   Entity regardingCase = service.Retrieve(regardingCaseRef.LogicalName, regardingCaseRef.Id, new ColumnSet(true));

                   //retrieve entityrecord from the ms crm server that match the

                   trace.Trace("4");

                   QueryExpression qeRelatedTasks = new QueryExpression("task");

                   qeRelatedTasks.ColumnSet = new ColumnSet(true);

                   qeRelatedTasks.Criteria.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Equal, regardingCaseRef.Id));

                   EntityCollection relatedTasks = service.RetrieveMultiple(qeRelatedTasks);

                   if (AllTaskReject(relatedTasks))

                   {

                       trace.Trace("status check");

                       regardingCase["statuscode"] = new OptionSetValue(912450000);

                       regardingCase["cos_csucasestatus"] = new OptionSetValue(769190006);

                       regardingCase["customersatisfactioncode"] = new OptionSetValue(5);

                       regardingCase["description"] = "All task update csutaskstatus";

                       trace.Trace("status check 2");

                       service.Update(regardingCase);

                       //update the case status == task status after check if

                   }

               }

               catch

               {

                   throw;

               }

           }

           public bool AllTaskReject(EntityCollection relatedTasks)

           {

               //check all the the task if rejected continue if on task not rejected do nothing

               if (relatedTasks.Entities.All(x => x.GetAttributeValue<OptionSetValue>("cos_csutaskstatus")?.Value == 769190003))

               {

                   return true;

               }

               else

               {

                   return false;

               }

    what do i need to change

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,986 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,588 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans