Hi,
I have replaced the OOTB address1_country field with a custom lookup list. I created an entity called Country, and created records of this type for every country in the world. Now, when the user creates a contact, they select the country from a lookup field.
However, when creating a contact in CRM by tracking it in from Outlook contacts, the original address1_country field is filled in. In order to then update the custom country field, I have written a plugin which takes the string value in address1_country (eg United Kingdom), looks up the relevant custom country record and adds it to the custom country field.
This all works perfectly when the string value matches a custom country. However, when the string value is something like 'UK' instead of 'United Kingdom', and therefore does not exist as a custom country record, I get the following error:
<Message>contact With Id = 2179c8b4-f2ff-e611-810e-e0071b6611e1 Does Not Exist</Message>
The plugin is firing on create of contact, post-operation, synchronous.
Could some suggest what I need to do?? Thank you.
Plugin code:
var pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (pluginExecutionContext.Depth > 1) { return; }
var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = factory.CreateOrganizationService(pluginExecutionContext.UserId);
Entity contact = pluginExecutionContext.InputParameters["Target"] as Entity;
ColumnSet cols = new ColumnSet("frp_country", "frp_country", "address1_country", "address1_country");
Entity contactfull = service.Retrieve("contact", contact.Id, cols);
string countryname = "";
Guid countryid = new Guid();
if (contactfull.Contains("frp_country")) { return; }
if (contactfull.Contains("address1_country")) { countryname = (string)contactfull["address1_country"]; } else { return; }
string fetchxml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='frp_countrylist'>
<attribute name='frp_countrylistid' />
<attribute name='frp_name' />
<filter type='and'>
<condition attribute='frp_name' operator='eq' value='" + countryname + @"' />
</filter>
</entity>
</fetch>";
EntityCollection results = service.RetrieveMultiple(new FetchExpression(fetchxml));
foreach (var ctry in results.Entities)
{
if (ctry.Contains("frp_countrylistid")) { countryid = (Guid)ctry["frp_countrylistid"]; }
}
contactfull["frp_country"] = new EntityReference("frp_countrylist", countryid);
service.Update(contactfull);
*This post is locked for comments