Sample Custom Workflow Code Dynamics 365 CE or Dataverse
Here is the Sample Custom Workflow Activities code for your usages. Modify the code as per your business need.
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
namespace ModernSchoolServerSideCode
{
public class getoutstandingamount : CodeActivity
{
[Input("Student Record")]
[ReferenceTarget("modern_student")]
public InArgument<EntityReference> TargetStudent { get; set; }
protected override void Execute(CodeActivityContext context)
{
IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
// Use the context service to create an instance of IOrganizationService.
IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.InitiatingUserId);
EntityReference currentStudentRecord = TargetStudent.Get(context);
var fetchXML = @"<fetch version='1.0' mapping='logical' no-lock='false' distinct='true'>
<entity name='modern_studentpayment'>
<attribute name='modern_studentpaymentid'/>
<attribute name='modern_name'/>
<attribute name='createdon'/>
<attribute name='modern_fees'/>
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0'/>
<condition attribute='modern_student' operator='eq' value='{0}' uitype='modern_student'/>
</filter>
</entity>
</fetch>";
fetchXML = string.Format(fetchXML, currentStudentRecord.Id);
EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXML));
decimal totaloutstandingamiunt = 0;
for (int i = 0; i < ec.Entities.Count; i++)
{
totaloutstandingamiunt = totaloutstandingamiunt + ((Money)ec.Entities[i].Attributes["modern_fees"]).Value;
}
OutAmount.Set(context, totaloutstandingamiunt);
}
[Output("OutstandingAmount")]
public OutArgument<decimal> OutAmount { get; set; }
}
}
Hope this helps.








