Tuesday, February 21, 2023

Sample Custom Workflow Code Dynamics 365 CE

 using System;

using System.Activities;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Newtonsoft.Json;

using Pelican.Falcon.Crm.Entities;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Query;

using Microsoft.Xrm.Sdk.Workflow;

using Microsoft.Crm.Sdk.Messages;

using System.Net.Http;

using System.ServiceModel;



namespace Pelican.Falcon.Crm.Workflow.GetRequestDetails

{

    public class RetriveRequestDetails : CodeActivity

    {

        

        [RequiredArgument]

        [Input("Request id")]

        [ReferenceTarget("plc_request")]

        public InArgument<EntityReference> Requestid { get; set; }


        [Output("Error Message")]

        public OutArgument<string> ErrorMessage { get; set; }


        [Output("Response Json")]

        public OutArgument<string> ResponseJson { get; set; }


        protected override void Execute(CodeActivityContext context)

        {

            ITracingService _traceservice = context.GetExtension<ITracingService>();

            IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();

            ITracingService trace = context.GetExtension<ITracingService>();

            IOrganizationService service = serviceFactory.CreateOrganizationService(context.GetExtension<IWorkflowContext>().InitiatingUserId);


            EntityReference currentrequestRecord = Requestid.Get(context);


            var fetchXML = @"<fetch version='1.0' mapping='logical' no-lock='false' distinct='true'>

                                <entity name='plc_request'>

                                    <attribute name='plc_requestid'/>

                                    <attribute name='plc_name'/>

                                    <attribute name='createdon'/>

                                    <attribute name='plc_jsonresponsedata'/>

                                    <filter type='and'>

                                        <condition attribute='statecode' operator='eq' value='0'/>

                                        <condition attribute='plc_requestid' operator='eq' value='{0}'  uitype='plc_request'/>

                                    </filter>

                                </entity>

                                </fetch>";

            fetchXML = string.Format(fetchXML, currentbillrequestRecord.Id);


            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetchXML));


            for (int i = 0; i < ec.Entities.Count; i++)

            {


                string strvalue = ec.Entities[i].Attributes["plc_jsonresponsedata"].ToString();

                string strname = ec.Entities[i].Attributes["plc_name"].ToString();                

                ResponseJson.Set(context, strvalue + "-" + strname);

                    

            }


           


        }

}

No comments:

Post a Comment

How to Deploy your API as a web app or API app

  Before you can call your custom API from a logic app workflow, deploy your API as a web app or API app to Azure App Service. To make your ...