Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

Pass a Guid through a Fetch XML Query for Custom Workflow

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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>";
*/

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Pass a Guid through a Fetch XML Query for Custom Workflow

    Hi guys so managed to figure this one out so thought I would post it for others to see

    Ended up being quite simple, just created a new Guid with using the Id of the entity refernce and then passed this through, I have highlighted it in bold

    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("Dummy Arg for Profiler")]
    [Default("Dummy Arg for Profiler")]
    public InArgument<string> DummyArg { get; set; }

    [Input("Advisor")]
    [ReferenceTarget("contact")]
    public InArgument<EntityReference> Advisor { 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 = Advisor.Get<EntityReference>(executionContext);

    //AdvisorStringGuid.Set(executionContext, advisorRef.Id);

    Guid advisorRefId = advisorRef.Id;


    //Create the fetch xml query
    string xml = @"<fetch distinct='false' mapping='logical' aggregate='true'>
    <entity name = 'icon_production'>
    <attribute name = 'icon_diftotalcurrentmonth' alias='advisorCurrentMonthValue' aggregate='sum'/>
    <filter type = 'and'>
    <condition attribute = 'createdon' operator= 'this-year'/>
    <condition attribute = 'icon_advisor' operator= 'eq' uitype = 'contact' value = '{"+ advisorRefId + @"}'/>
    </filter>
    </entity>
    </fetch> ";

    //Create the entity collection
    EntityCollection currentYearTotal = service.RetrieveMultiple(new FetchExpression(xml));

    foreach (var c in currentYearTotal.Entities)
    {
    decimal aggregate = ((decimal)((AliasedValue)c["advisorCurrentMonthValue"]).Value);
    YearToDate.Set(executionContext, aggregate);
    }
    }
    catch (Exception e)
    {
    throw new InvalidPluginExecutionException(e.Message);
    }
    }
    }
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans