Hello Team,
I have a requirement to create a plugin (using .Net), that would use the Task Due Date to update a custom field (Next Activity Date) in Account, Opportunity, Contact and Cases. I first created a Custom Activity Workflow and when it works I will convert
it to a plugin. The Custom Workflow Activity runs successfully, but does not update the fields in other Entities. Does anyone have an idea how I can pass data from Task
to Update fields in other entities?
Here some segments of my code:
1) Each secondary entity (Account, Contact, Opportunity and Cases, have these code blocks)
QueryExpression qeOpportunity = new QueryExpression();
qeOpportunity.EntityName = "opportunity";
qeOpportunity.ColumnSet = new ColumnSet(true);
qeOpportunity.Criteria = new FilterExpression();
qeOpportunity.Criteria.AddCondition("statecode", ConditionOperator.Equal,
0);
qeOpportunity.Criteria.AddCondition("name", ConditionOperator.Equal,
Task["regardingobjectid"]);
EntityCollection ecOpportunity
= _service.RetrieveMultiple(qeOpportunity);
tracingService.Trace("opptynextactivitydate
has been retrieved");
if (Oppty.Contains("name"))
{
checkNextActivityDate = (DateTime)Acc["esi_nextactivitydate"];
if ((checkNextActivityDate
!= null) && (checkNextActivityDate < DateTime.Now))
{
Oppty["esi_nextactivitydate"] = null;
_service.Update(Oppty);
tracingService.Trace("opptynextactivitydate has been set to null");
}
foreach (var opptyentity in ecOpportunity.Entities)
{
dates.Add(Convert.ToDateTime(opptyentity.Attributes["scheduledend"]));
}
}
2) I then pass the final values using these codes:
var allDates = dates.OrderBy(d => d).ToList();
if (dates.Count
== 1)
{
if ((DateTime)dates[0]
>= DateTime.Now)
{
Acc["esi_nextactivitydate"] = dates[0];
Acc["accountid"] = new EntityReference("task",
context.PrimaryEntityId);
_service.Update(Acc);
tracingService.Trace("Account next activity date is updated");
Oppty["esi_nextactivitydate"] = dates[0];
Oppty["opportunityid"] = new EntityReference("task",
context.PrimaryEntityId);
_service.Update(Oppty);
tracingService.Trace("Opportunity next activity date is updated");
CaseRecords["esi_nextactivitydate"] = dates[0];
CaseRecords["incidentid"] = new EntityReference("task",
context.PrimaryEntityId);
_service.Update(CaseRecords);
tracingService.Trace("Case next activity date is updated");
Cntacts["esi_nextactivitydate"] = dates[0];
Cntacts["contactid"] = new EntityReference("task",
context.PrimaryEntityId);
_service.Update(Cntacts);
tracingService.Trace("Contact next activity date is updated");
return;
}
}
}
Thank you in advance for your help.
LOE