IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity Lead = new Entity("lead");
string emailid = null;
int index;
string domain;
string[] domains = new string[10];
string[] publicdomains = { "aol", "att", "comcast", "facebook", "gmail", "gmx", "googlemail", "google", "hotmail", "hotmail", "mac", "me", "mail", "msn", "live", "sbcglobal", "verizon", "yahoo", "yahoo", "email", "games", "gmx", "hush", "hushmail", "icloud", "inbox", "lavabit", "love", "outlook", "pobox", "rocketmail", "safe-mail", "wow", "ygm", "ymail", "zoho", "fastmail", "yandex", "bellsouth", "charter", "comcast", "cox", "earthlink", "juno", "btinternet", "virginmedia", "blueyonder", "freeserve", "live", "ntlworld", "o2", "orange", "sky", "talktalk", "tiscali", "virgin", "wanadoo", "bt", "sina", "qq", "naver", "hanmail", "daum", "nate", "yahoo", "yahoo", "yahoo", "yahoo", "yahoo", "yahoo", "hotmail", "live", "laposte", "yahoo", "wanadoo", "orange", "gmx", "sfr", "neuf", "free", "gmx", "hotmail", "live", "online", "t-online", "web", "yahoo", "mail", "rambler", "yandex", "ya", "list", "hotmail", "live", "skynet", "voo", "tvcablenet", "telenet", "hotmail", "live", "yahoo", "fibertel", "speedy", "arnet", "hotmail", "gmail", "yahoo", "live", "yahoo", "hotmail", "live", "hotmail", "prodigy", "msn" };
bool found = false;
Entity ent = (Entity)context.InputParameters["Target"];
Lead = service.Retrieve("lead", ent.Id, new ColumnSet(true));
if(Lead.LogicalName != "lead")
{
return;
}
if(Lead.Attributes.Contains("emailaddress1"))
{
emailid = Lead.Attributes["emailaddress1"].ToString();
index = emailid.IndexOf("@");
domain = emailid.Substring(index + 1, emailid.Length - index - 1);
domains = domain.Split('.');
foreach (var item in publicdomains)
{
if(domains[0] == item)
{
found = true;
break;
}
}
}
else
{
emailid = string.Empty;
}
if (emailid != string.Empty)
{
QueryExpression checkcontact = new QueryExpression
{
EntityName = "contact",
ColumnSet = new ColumnSet("contactid", "ownerid", "parentcustomerid")
};
ConditionExpression checkemailid = new ConditionExpression();
checkemailid.AttributeName = "emailaddress1";
checkemailid.Operator = ConditionOperator.Equal;
checkemailid.Values.Add(emailid);
checkcontact.Criteria.AddCondition(checkemailid);
EntityCollection retrievecontact = service.RetrieveMultiple(checkcontact);
if (retrievecontact.Entities.Count > 0)
{
foreach (var item in retrievecontact.Entities)
{
EntityReference entref = (EntityReference)item.Attributes["ownerid"];
var id = entref.Id;
Lead.Attributes["ownerid"] = new EntityReference("systemuser", id);
EntityReference accountdetails = (EntityReference)item.Attributes["parentcustomerid"];
var accountid = accountdetails.Id;
Lead.Attributes.Add("parentaccountid", new EntityReference("account",accountid));
Lead.Attributes.Add("parentcontactid", new EntityReference("contact", (Guid)item.Attributes["contactid"]));
service.Update(Lead);
}
}
else
{
QueryExpression checkaccount = new QueryExpression
{
EntityName = "account",
ColumnSet = new ColumnSet("accountid", "ownerid")
};
ConditionExpression checkdomain = new ConditionExpression();
checkdomain.AttributeName = "new_domain";
checkdomain.Operator = ConditionOperator.Equal;
checkdomain.Values.Add(domains[0]);
checkaccount.Criteria.AddCondition(checkdomain);
if (found == false)
{
EntityCollection retrieveaccount = service.RetrieveMultiple(checkaccount);
if (retrieveaccount.Entities.Count > 0)
{
foreach (var item in retrieveaccount.Entities)
{
EntityReference entref = (EntityReference)item.Attributes["ownerid"];
var id = entref.Id;
Lead.Attributes["ownerid"] = new EntityReference("systemuser", id);
Lead.Attributes.Add("parentaccountid", new EntityReference("account", (Guid)item.Attributes["accountid"]));
service.Update(Lead);
}
}
}
}
}
}
}
catch(InvalidPluginExecutionException ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}