Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Dynamics CRM- update contact entity record

Posted on by 270

Hi,

I have created contact record using asp.net. Now i need to Check if the contact record exists . If exists , update the same record. through advance find have downloaded fetch XML and added to my fetchxml variable. please suggest the logic .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=xyz ;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 = @" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='contact'>
<attribute name='fullname' />
<attribute name='parentcustomerid' />
<attribute name='telephone1' />
<attribute name='emailaddress1' />
<attribute name='contactid' />
<order attribute='fullname' descending='false' />
<filter type='and'>
<condition attribute= 'mobilephone' operator='not-null' />
</filter>
</entity>
</fetch>" ;
FetchExpression query = new FetchExpression(fetchXml);
EntityCollection results = service.RetrieveMultiple(query);

if (results.Entities.Count > 0)
{
Entity contactRecord = results[0];

contactRecord["firstname"] = contactInfo.FirstName;
contactRecord["lastname"] = contactInfo.LastName;
contactRecord["emailaddress1"] = contactInfo.EmailId;
contactRecord["mobilephone"] = contactInfo.MobilePhone;
contactRecord["address1_line1"] = contactInfo.Street1;
contactRecord["address1_line2"] = contactInfo.Street2;
contactRecord["address1_line3"] = contactInfo.Street3;
contactRecord["address1_city"] = contactInfo.City;

service.Update(contactRecord);


}


//Else, Create the contact record
else
{
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);
}
else
{
entity["gendercode"] = new OptionSetValue(2);
}

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

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

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

service.Create(entity);
}
}





// Create the entity
}

}
}

*This post is locked for comments

  • Suggested answer
    Hosk Profile Picture
    Hosk on at
    RE: Dynamics CRM- update contact entity record

    right ok

    at the moment you don't seem to have any filter condition on your FetchXML, I am assuming you don't want to return all the contacts

    The easiest way to work out what FetchXML you need is to do the query in an advanced find in CRM.

    Then you can check to see if the results bring a record back and if they do the record will exist in CRM, so you will need to update it

  • Suggested answer
    Sayhaitokumar Profile Picture
    Sayhaitokumar 7,042 on at
    RE: Dynamics CRM- update contact entity record

    Hi Pulkita Chauhan,

    Please refer the following link. It may help you.

    msdn.microsoft.com/.../gg328090.aspx

  • Pulkita Chauhan Profile Picture
    Pulkita Chauhan 270 on at
    RE: Dynamics CRM- update contact entity record

    Hi,

    yes, code is working for creating records in contact entity.

    Now, challenge is to update the existing contact records but before that need to check if the contact record exists . If exists , update the same record.

    Pulkita Chauhan

  • Hosk Profile Picture
    Hosk on at
    RE: Dynamics CRM- update contact entity record

    what is your actual question?

    is the code working?

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans