Hi guys
Been struggling with this for a while, I am getting a lookup field(to contact entity) as an input argument to my custom workflow, i am then using a fetch xml query to update a field within a custom entity called "icon_production" I am struggling to pass the guid through as a variable, code is below any help would be much appreciated!
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
namespace Icon.Sanlam.Workflows
{
public class Production_UpdateYearToDate : CodeActivity
{
[Input("Advisor")]
[ReferenceTarget("contact")]
public InArgument<EntityReference> Advisor { get; set; }
//Output argument to get the string
[Output("UpdateStringGUID")]
public OutArgument<string> UpdateStringGUID { get; set; }
//Output argument for advisor
[Output("Year To Date")]
public OutArgument<decimal> YearToDate { get; set; }
protected override void Execute(CodeActivityContext executionContext)
{
ITracingService tracer = executionContext.GetExtension<ITracingService>();
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
//Get the values
//EntityReference advisorRef = AdvisorId.Get(executionContext);
EntityReference advisorRefID = Advisor.Get<EntityReference>(executionContext);
string advisorRefIDString = advisorRefID.Id.ToString();
UpdateStringGUID.Set(executionContext, advisorRefID.Id.ToString());
//**Special Note** Input must not contain outer brackets {}
string xml = String.Format(@"<fetch mapping='logical' distinct='false' aggregate='true'>" +
"<entity name='icon_production'>" +
"<attribute name='icon_diftotalcurrentmonth' aggregate='sum' alias='advisorCurrentMonthValue' />" +
"<filter type='and'>" +
"<condition attribute='createdon' operator='this-year' />" +
"<condition attribute='icon_advisor' operator='eq' uitype='contact' value=' {" + advisorRefIDString + "} ' /> " +
"</filter>" +
"</entity>" +
"</fetch>");
//Create the entity collection
EntityCollection currentYearTotal = service.RetrieveMultiple(new FetchExpression(xml));
foreach (var c in currentYearTotal.Entities)
{
decimal totalCurrentYear = ((decimal)((AliasedValue)c["advisorCurrentMonthValue"]).Value);
YearToDate.Set(executionContext, totalCurrentYear);
}
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.Message);
}
}
}
}
/*
<fetch mapping='logical' distinct='false' aggregate='true'>
<entity name='icon_production'>
<attribute name='createdon' />
<attribute name='icon_advisor' />
<attribute name='icon_diftotalcurrentmonth' aggregate='sum' alias='advisorCurrentMonthValue' />
<order attribute='createdon' descending='false' />
<filter type='and'>
<condition attribute='createdon' operator='this-year' />
<condition attribute='icon_advisor' operator='eq' uitype='contact' value='{1C1488B3-E2B8-E911-A824-000D3AB2DE70}' />
</filter>
</entity>
</fetch>";
*/