Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Check for existing contact causes 'contact.emailaddress1': null is not a valid value for an attribute

Posted on by 255

Hi

I check for an existing contact following form submissions to add a contact before adding a new contact.

The below snippet sort of works but I get regular errors as follows

"Condition for attribute 'contact.emailaddress1': null is not a valid value for an attribute. Use 'Null' or 'NotNull' conditions instead"

Can you tell me what to change?

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 would this work 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();
						}
					}
			} 


*This post is locked for comments

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans