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
Hi ,
Do you want to through exception if email address is matching or anything else ?
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;
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
Ben, can that same code return the existing contactid ?
ec.Entities("contactid")
it will have it already using ec.Entities[0].Id if a record is returned....
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(); } } }
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Community Member 2
HR-09070029-0 2
UllrSki 2