Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plugin query - Contact with Id = xxx-xxxx does not exist

(0) ShareShare
ReportReport
Posted on by

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

  • RE: Plugin query - Contact with Id = xxx-xxxx does not exist

    That works perfectly! Thank you!

  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Plugin query - Contact with Id = xxx-xxxx does not exist

    Hello,

    You should not use Update of contact. Try to rebuild your plugin using pre-operation and using following code instead:

    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;
    
    if (contact.Contains("frp_country"))
    	return;
    
    var countryname = contact.GetAttributeValue<string>("address1_country");
    
    if (string.IsNullOrEmpty(countryname))
    	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>";
    
    var country = service.RetrieveMultiple(new FetchExpression(fetchxml)).Entities.FirstOrDefault();
    
    if (country == null)
    	return;
    
    contact["frp_country"] = conutry.ToEntityReference();


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,403 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans