web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Check for existing contact email address

(0) ShareShare
ReportReport
Posted on by 255

Hi

Whats the simplest way to check for a contact entity using email address in c#.

A simple true or false will suffice then I can do an Update rather than a Create.

Many thanks

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Pawar Pravin  Profile Picture
    5,237 on at
    RE: Check for existing contact email address

    Hi ,

    Do you want to through exception if email address is matching or anything else ?

  • Verified answer
    Ben Thompson Profile Picture
    6,350 on at
    RE: Check for existing contact email address

    The quickest and easiest way would be to ensure email address is an alternate key on the contact entity and then to use an upsert request.

    The other way would be to query the email address in a retrieve multiple and check if any results are returned - something like this should work

    var QEcontact = new QueryExpression("contact");
    QEcontact.TopCount = 50;
    
    QEcontact.ColumnSet= new ColumnSet("emailaddress1");
    
    // Define filter QEcontact.Criteria
    QEcontact.Criteria.AddCondition("emailaddress1", ConditionOperator.Equal, "email@domain.com");
    EntityCollection ec =service.RetrieveMultiple(QEcontact);
    if (ec.Entities!=null && ec.Entities.Count>0){
    return true;
    }
    return false;
  • Stuie Profile Picture
    255 on at
    RE: Check for existing contact email address

    I just want to check if a contact exists before I create it. It the email already exists in contacts then Ill do an Update rather than a Create

  • Stuie Profile Picture
    255 on at
    RE: Check for existing contact email address

    Ben, can that same code return the existing contactid ?

    ec.Entities("contactid")

  • Suggested answer
    Ben Thompson Profile Picture
    6,350 on at
    RE: Check for existing contact email address

    it will have it already using ec.Entities[0].Id if a record is returned....

  • Stuie Profile Picture
    255 on at
    RE: Check for existing contact email address

    Hi Ben

    Would this be ok?

                
    var QEcontact = new QueryExpression("contact"); QEcontact.TopCount = 50; QEcontact.ColumnSet = new ColumnSet("emailaddress1", "contactid", "statecode"); QEcontact.Criteria.AddCondition("emailaddress1", ConditionOperator.Equal, Email); EntityCollection ec = service.RetrieveMultiple(QEcontact); if (ec.Entities != null && ec.Entities.Count > 0) { contactExists = 1; currentUser = ec.Entities[0].Id; sCurrentStatus = ""; foreach (var c in ec.Entities) { currentStatus = c.FormattedValues["statecode"]; sCurrentStatus = currentStatus.ToString(); } }

    Or is this better?

             QueryExpression query = new QueryExpression
    			{
    				EntityName = "contact",
    				ColumnSet = new ColumnSet("emailaddress1", "contactid", "statecode"),
    				Criteria = new FilterExpression
    				{
    					Conditions = { new ConditionExpression { AttributeName = "emailaddress1", Operator = ConditionOperator.NotNull } }
    				}
    			};
    			EntityCollection ec = service.RetrieveMultiple(query);
    			if (ec.Entities.Count > 0) //Check for EntityCollection count
    			{
    				foreach (var item in ec.Entities)
    					{
    						if (item.Attributes["emailaddress1"].ToString() == Email)
    						{
    							contactExists = 1;
    							currentUser = ec.Entities[0].Id;
    							currentStatus = c.FormattedValues["statecode"];
    		                    sCurrentStatus = currentStatus.ToString();
    						}
    					}
    			} 


    Many thanks

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans