THIS IS MY CODE
MY IS BELOW:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace ActionStepsExecutionStatusDecisionPlugin
{
public class ActionStepsExecution : 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);
try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity _target = (Entity)context.InputParameters["Target"];
EntityReference regarding = null;
QueryExpression querytask = new QueryExpression(_target.LogicalName);
querytask.Criteria.AddCondition("activityid", ConditionOperator.Equal, _target.Id);
querytask.ColumnSet = new ColumnSet("regardingobjectid");
EntityCollection task_entity = service.RetrieveMultiple(querytask);
foreach (Entity task_res in task_entity.Entities)
{
regarding = (EntityReference)task_res.Attributes["regardingobjectid"]; //ActionSteps
}
if (regarding.LogicalName == "qbd_actionsteps")
{
string InputField = string.Empty;
Entity action_entity = service.Retrieve(regarding.LogicalName, regarding.Id, new ColumnSet("qbd_actionstepssource"));//Retrive source field schema name
if (action_entity.Contains("qbd_actionstepssource"))
{
InputField = action_entity.GetAttributeValue<string>("qbd_actionstepssource");
}
else
{
throw new InvalidPluginExecutionException("Action Steps Source cannot be null");
}
EntityReference parent_entityref = null;
#region Get Total Task Count
QueryExpression querycount = new QueryExpression(_target.LogicalName);
querycount.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, regarding.Id);
querycount.ColumnSet = new ColumnSet("subject");
EntityCollection taskcount = service.RetrieveMultiple(querycount);
int task_count = taskcount.Entities.Count;
#endregion
#region Task Loop - Action Step Status Update
QueryExpression query_as = new QueryExpression(regarding.LogicalName);//ActionSteps
query_as.Criteria.AddCondition("qbd_actionstepsid", ConditionOperator.Equal, regarding.Id);
query_as.ColumnSet = new ColumnSet("qbd_name", "qbd_actionsteptaskstatus", InputField);//qbd_actionstepsidcc
EntityCollection asfetch = service.RetrieveMultiple(query_as);
foreach (Entity actionsteps in asfetch.Entities)
{
parent_entityref = (EntityReference)actionsteps.Attributes[InputField];
#fields update
}
}
#endregion
#region Action Step Loop - Parent Entity Status Update
Entity parent_entity = service.Retrieve(parent_entityref.LogicalName, parent_entityref.Id, new ColumnSet("qbd_name", "qbd_executionstatus"));//Retrive Parent
QueryExpression query_actions = new QueryExpression(regarding.LogicalName);//ActionSteps
query_actions.Criteria.AddCondition(InputField, ConditionOperator.Equal, parent_entityref.Id);
query_actions.ColumnSet = new ColumnSet("qbd_name", "qbd_actionsteptaskstatus", InputField);
EntityCollection actionsfetch = service.RetrieveMultiple(query_actions);
foreach (Entity all_actionsteps in actionsfetch.Entities)
{
#fields update
}
#endregion
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}