using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using System.Linq;
namespace Sample
{
[CrmPluginRegistration(
"Sample.ParseContact", "ParseContact", "Parses contact names from text following the specified keywords", "Sample", IsolationModeEnum.Sandbox
)]
public class ParseContact : CodeActivity
{
[Input("Text")]
public InArgument<string> Text { get; set; }
[Input("Keywords")]
public InArgument<string> Keywords { get; set; }
[Output("ContactFirstName")]
public OutArgument<string> ContactFirstName { get; set; }
[Output("ContactLastName")]
public OutArgument<string> ContactLastName { get; set; }
protected override void Execute(CodeActivityContext context)
{
// Initializes trace, service, serviceFactory, workflowContext
var service = context.GetExtension<IOrganizationService>();
var text = context.GetValue<string>(Text);
var keyword = context.GetValue<string>(Keywords);
if (string.IsNullOrWhiteSpace(text) || string.IsNullOrWhiteSpace(keyword))
return;
var foundAt = text.IndexOf(keyword, StringComparison.OrdinalIgnoreCase);
if (foundAt < 0)
return;
var textFollowingFound = text.Substring(foundAt + keyword.Length).Trim();
var words = textFollowingFound.Split(' ').ToList();
context.SetValue<string>(ContactFirstName, ReplaceInvalidChars(words.ElementAtOrDefault(0)));
context.SetValue<string>(ContactLastName, ReplaceInvalidChars(words.ElementAtOrDefault(1)));
}
private string ReplaceInvalidChars(string input)
{
return input?.Replace(".", "").Replace("?", "");
}
}
}