Hi Everyone,
I am trying to create a custom workflow which will search through the email bodies text and find if they contain any Incident Number reference. If they do then it will give as an output the Case Entity with that specific Incident Number.
When i am trying to register my custom workflow though, it gives me an error "Invalid CrmReferenceTarget: The entity Incident doesn't exist. "
Can someone please help me with this?
The code for the plugin is the following:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Text.RegularExpressions;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmailSubject
{
public class EmailTelcoReference : CodeActivity
{
[Output("TelcoReferenceNumber")]
[ReferenceTarget("Incident")]
public OutArgument<EntityReference> TelcoReferenceNumber { get; set; }
[Input("EmailSubject")]
[ReferenceTarget("Subject")]
//[Default("INC0600612 - [UID:42567] [PID:Mobile] CAS-27673-M6X3K7 - Sim number check CRM:0008006")]
[RequiredArgument]
public InArgument<String> Email { get; set; }
protected override void Execute(CodeActivityContext Context)
{
//Creating the context
ITracingService tracingService = Context.GetExtension<ITracingService>();
IWorkflowContext context = Context.GetExtension<IWorkflowContext>();
if (Context != null) { tracingService.Trace("Context created"); }
IOrganizationServiceFactory serviceFactory = Context.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (service != null) { tracingService.Trace("Organization Service created"); }
//string sPattern = "^.*INC*[0-9]{6,8}\b$";
Regex re = new Regex(@".*INC*[0-9]{6,8}\b", RegexOptions.IgnoreCase);
var inputSubject = Email.Get(Context);
try
{
if (re.IsMatch(inputSubject))
{
var incidentNumber = re.Match(inputSubject).ToString();
Entity entity = (Entity)context.InputParameters[incidentNumber];
if(entity.LogicalName == "Incident")
{
EntityReference caseEntity = entity.ToEntityReference();
TelcoReferenceNumber.Set(Context, caseEntity);
}
else
{
TelcoReferenceNumber.Set(Context, null);
}
}
}
catch (ArgumentException)
{
TelcoReferenceNumber.Set(Context, null);
}
}
}
}