Check if the contact record exists . If exists , update the same record. RRS feed

  • Question

  • Hi,

    I have created a landing page (web form) using ASP.net and Establish a connection with dynamics CRM to get the connection proxy .Now i have to check if the contact record exists . If exists , update the same record.please help me in code. need it really urgently.

    below is my code:

    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.Xrm.Client;
    using Microsoft.Xrm.Client.Services;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace LandingPage
        public class CrmDataProvider
            public void CreateContact(ContactDetails contactInfo)

                    // Establish a connection to crm and get the connection proxy 

                string connectionString = "Url= xyz ; Username=abc ;Password=xyz";
                    CrmConnection connect = CrmConnection.Parse(connectionString);
                    OrganizationService service;

                    using (service = new OrganizationService(connect))
                        WhoAmIRequest request = new WhoAmIRequest();
                        Guid userId = ((WhoAmIResponse)service.Execute(request)).UserId;

                        ContactDetails contact = new ContactDetails();
                        //Check if the contact record exists . If exists , update the same record.

                        //Fecthxml query 
                        string fetchXml = string.Empty;

                        FetchExpression query = new FetchExpression(fetchXml);

                        EntityCollection results = service.RetrieveMultiple(query);
                        if (results.Entities.Count > 0)
                            Entity contactRecord = results[0];


                        //Else, Create the contact record

                        Entity entity = new Entity();
                        entity.LogicalName = "contact";

                        entity["firstname"] = contactInfo.FirstName;
                        entity["lastname"] = contactInfo.LastName;
                        entity["emailaddress1"] = contactInfo.EmailId;
                        entity["mobilephone"] = contactInfo.MobilePhone;
                        entity["address1_line1"] = contactInfo.Street1;
                        entity["address1_line2"] = contactInfo.Street2;
                        entity["address1_line3"] = contactInfo.Street3;
                        entity["address1_city"] = contactInfo.City;
                        entity["address1_stateorprovince"] = contactInfo.State;
                        entity["address1_country"] = contactInfo.Country;
                        entity["spousesname"] = contactInfo.SpouseName;
                        entity["birthdate"] = contactInfo.Birthday;
                        entity["anniversary"] = contactInfo.Anniversary;
                        //Create entity gender with option value
                        if (contactInfo.Gender == "Male")
                            entity["gendercode"] = new OptionSetValue(1);
                            entity["gendercode"] = new OptionSetValue(2);

                      //entity["familystatuscode"] = contactInfo.MaritalStatus;

                        if (contactInfo.MaritalStatus == "Single")
                            entity["familystatuscode"] = new OptionSetValue(1);

                            entity["familystatuscode"] = new OptionSetValue(2);


                // Create the entity 




    Thursday, September 25, 2014 9:22 AM

All replies

  • now, go to advanced find, create the search with the filter condition to check if a record exists. Then download the fetch xml and add it to your fetchXml variable.

    see if it works. Before calling update, you have to assign value to the attributes.

    let me know how it goes.



    Thursday, September 25, 2014 10:37 AM
  • hi,

    thanks jithesh, i am newbie in dynamics crm it would be great if u cud tell me how add downloaded fetch xml to my fetch xml variable..



    Thursday, September 25, 2014 11:47 AM
  • What is the criteria for duplicate contact like firstname and lastname or firstname lastname and dateofbirth?

    Regards Faisal

    Thursday, September 25, 2014 12:34 PM