Tuesday, 20 August 2013

Custom Workflow Activity: To Retrieve GUID and Entity Name from a Lookup field

Below is a custom workflow activity that can be used to retrieve the GUID and the Entity Logical Name of a Lookup field, from the record that is calling the workflow. The output of this custom workflow activity can then be used in the workflow.

Inputs:
  • Attribute Name: Name of the Lookup attribute. If the input is blank, the ID and Entity Logical Name of the record calling the workflow will be returned as the Output.
Output:
  • Entity Name and Record ID of the Lookup field or the record calling the workflow.


using System;
using System.Activities;
using System.Linq;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;

namespace KPY.WorkflowUtilities
{
    public class GetLookupAttributeID : CodeActivity
    {
        [Input("Attribute Name (If blank, the originating record will be displayed)")]
        public InArgument<string> AttributeName { get; set; }

        [Output("Entity Logical Name")]
        public OutArgument<string> EntityName { get; set; }

        [Output("Record ID")]
        public OutArgument<string> RecordID { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            IWorkflowContext wfcontext = context.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory factory = context.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = factory.CreateOrganizationService(wfcontext.InitiatingUserId);

            string strAttributeName = AttributeName.Get(context);

            if (string.IsNullOrEmpty(strAttributeName))
            {
                // -- Get from current context Entity --
                Guid EntID = wfcontext.PrimaryEntityId;
                string EntName = wfcontext.PrimaryEntityName;

                EntityName.Set(context, EntName);
                RecordID.Set(context, EntID.ToString());
            }
            else
            {
                if (wfcontext.MessageName == "Delete")
                {
                    // -- Get from Pre Image --
                    Entity preImageEnt = wfcontext.PreEntityImages.Values.FirstOrDefault();
                    if (preImageEnt != null && preImageEnt.Attributes.Contains(strAttributeName) && preImageEnt[strAttributeName] != null)
                    {
                        EntityReference entRef = (EntityReference)preImageEnt[strAttributeName];
                        EntityName.Set(context, entRef.LogicalName);
                        RecordID.Set(context, entRef.Id.ToString());
                    }
                }
                else
                {
                    // -- Get from post image --
                    Entity postImageEnt = wfcontext.PostEntityImages.Values.FirstOrDefault();
                    if (postImageEnt != null && postImageEnt.Attributes.Contains(strAttributeName) && postImageEnt[strAttributeName] != null)
                    {
                        EntityReference entRef = (EntityReference)postImageEnt[strAttributeName];
                        EntityName.Set(context, entRef.LogicalName);
                        RecordID.Set(context, entRef.Id.ToString());
                    }
                }
            }
        }
    }
}


No comments:

Post a Comment