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.
- 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