Using Upsert in Microsoft Dynamics CRM 2015 Update 1


Another useful feature that is introduced in Dynamics CRM 2015 update 1 is support for upsert operation. Upsert allows us to create or update record in CRM in single call, so we don’t need to bother if there is any existing record with the same keys or combination of other fields. This is specially useful in case of integration, when we are not sure that record from other system is already integrated with CRM or not. We can use upsert with alternate keys, Please refer our earlier post for how to use alternate keys. Let’s take below example where we are writing a account request, using upsertrequest,

Entity account = new Entity(“account”, “accountnumber”, “UPST601”);
account[“name”] = “Upsert Demo”;
account[“revenue”] = new Money(5000);

UpsertRequest request = new UpsertRequest()
{
Target = account
};

UpsertResponse response = (UpsertResponse)service.Execute(request);
if (response.RecordCreated)
Console.WriteLine(account[“name”] + ” Created with Revenue: ” + account.GetAttributeValue<Money>(“revenue”).Value);
else
Console.WriteLine(account[“name”] + ” Updated with Revenue: ” + account.GetAttributeValue<Money>(“revenue”).Value);

Console.WriteLine(“———————————–“);
account[“revenue”] = new Money(15000);
UpsertRequest request1 = new UpsertRequest()
{
Target = account
};
response = (UpsertResponse)service.Execute(request);
if (response.RecordCreated)
Console.WriteLine(account[“name”] + ” Created with Revenue: ” + account.GetAttributeValue<Money>(“revenue”).Value);
else
Console.WriteLine(account[“name”] + ” Updated with Revenue: ” + account.GetAttributeValue<Money>(“revenue”).Value);
Console.ReadLine();

As you can see in above code we are executing upsert request two times, so first time as this record is not available in CRM, it will create it, but while executing next statement it will update existing record instead of creating it,

upsert

HIMBAP | Need any help in Microsoft CRM 2015 Development Contact US !!

Advertisements

Leave a comment

Filed under MS CRM 2015 Update 1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s