Views:

UPSERT Command:

To create a record using create operation and update a record using an update operation. These are the two operations needed to be performed by the user manually when required.

For instance, when a user wants to create a record and user might be unaware of the records that are existing in CRM already. In order to do that user needs to retrieve all the existing records from the entity to create a record if it is not existed or update a record if it exists. This is a time consuming scenario as decision needs to be taken each time whenever an operation is to be performed.

User won’t be able to perform any operation (i.e. create or update) unless and until a selection of records is done manually by the user.

To overcome such complexity latest feature is introduced in CRM 2015 updated 1 with a command called “UPSERT”.

System Requirements to work UPSERT Command:

  • Microsoft Dynamics CRM Online 2015 Update 1 organization.
  • Microsoft.CrmSdk.CoreAssemblies version 7.1.0-preview NuGet Package.
  • Visual Studio FrameWork Version 4.5.2.
  •  

Functioning Of UPSERT:

UPSERT command is a combination of Create or an Update operation. It performs create or update operation based on the condition given by the user. UPSERT performs an operation each time when the record is found that exists in the CRM.

  • Use Create Operation for creating records
  • Use UPSERT request for updating the records
  • In CRM to implement UPSERT, user need to use the concept of 'Keys' .
  • The Key concept is introduced in MS CRM Online 2015 Update 1.
  • User should create a key or keys by associating the required fields of the entity in the CRM.
  • The specific key can be used to work with UPSERT command which will be treated as primary key that helps in checking whether the record exists or not.
  • In the Keys, user provides the fields/value pairs to look for a matching record.
  • You cannot use any combination of field/value pairs. The field/value pair provided should be a part of Key that already defined for the entity.
  • For function of UPSERT command we have two classes called UpsertRequest and UpsertResponse.
  • UPSERT command contains important property called "Target”. The Target property contains all attributes information including record id of the entity.

 

This Target property will be declared in UpsertRequest.And this UpsertRequest will pass to UpsertResponse.

Example Programming Flow For Working of UPSERT Command:

private void UpsertButton_Click(object sender, EventArgs e)

{

service = ServiceHelper.ServiceProxy;

// Creating Entity object and intialising Key attribute and its value.

Entity account = new Entity("account", "accountnumber", AccountNumber);

//Initialising Account entity attributes either to perform create or update operation.

account ["accountnumber"] = AccountNumbertb.Text.ToString();

account["name"] = AccountNametb.Text.ToString();

account["new_mobilenumber"] = MobileNumbertb.Text.ToString();

//Creating UpsertRequest object and initialising Upsert command property called 'Target' which holds entity attributes information.

UpsertRequest request = new UpsertRequest()

{

Target = account

};

//Creating UpsertResponse object and passing UpsertRequest object which containing Target.

UpsertResponse response = (UpsertResponse)service.Execute(request);

if (response.RecordCreated){

Console.WriteLine(" Created with Mobile Number :);

else

Console.WriteLine(" Updated with Mobile Number : " );

Console.ReadLine();

}

//RecordCreated this property is used to know whether the record is created or updated.RecordCreated is true if the record didn’t exist and is created. It is false if the record already existed and is updated.