Showing posts with label Upsert. Show all posts
Showing posts with label Upsert. Show all posts

Wednesday, October 19, 2016

Upsert Request using Alternative key

In MS CRM 2016, now we can insert or update request in a single request called Upsert. 
If record with particular key already exists in system, then it will be updated, if not then new record will be created. 

 More details are available  on MSDN site

https://msdn.microsoft.com/en-us/library/dn932135.aspx

Now in CRM 2016 we can create alternative keys, which are either single attribute or combination of attributes, you can use this alternative key for insert / update record. 


1. Single Attribute in alternative Key upsert

I created custom entity called Car, with attributes car no (whole number), name (string), make year (whole number) and Manufacture (string). And defined alternative key on car no. 


My Alternative Key name is - new_carNoId

When using Upsert request, we need to specify attribute name in request NOT key name. 
Here is sample code

Entity car = new Entity(new_car.EntityLogicalName, "new_carno", 2);
car["new_name"] = "Carolla";
car["new_make"] =2015;
car["new_manufacture"] = "Toyota";

UpsertRequest req = new UpsertRequest()
 {
   Target = car
 };


 UpsertResponse resp = (UpsertResponse) serviceProxy.Execute(req);


When creating entity object for insert / update, we need to specify alternate key attribute name and value, and then set other required attributes of record. 

Note: When you create alternative key, that key must be active to use Upsert, if alternative key status pending / failed, execute method will throw error. 

2. Multiple Attribute in alternative Key upsert
If you have alternative key with multiple attributes, then you need to initialize entity object differently. 

I created alternative key in my car entity with Car No (whole Number) and Manufacture (string) attributes. 

When initializing entity object at that time we need to specify KeyAttributeCollection 
and add all your key attribute in this collection. 

 KeyAttributeCollection keyColl = new KeyAttributeCollection();
     keyColl.Add("new_carno", objcar.carNo);
     keyColl.Add("new_manufacture", objcar.Manufacture);

     Entity car = new Entity(new_car.EntityLogicalName, keyColl);
     car["new_name"] = objcar.CarName;
     car["new_make"] = objcar.MakeYear;

     UpsertRequest req = new UpsertRequest()
       {
         Target = car
       };
     UpsertResponse resp = (UpsertResponse) serviceProxy.Execute(req);

When Upsert request is executed, at that time CRM will look for combination of Car No and Manufacture, if any record exists in system, then it will be updated, otherwise it record will be created. 

If you have lots of records and wants to insert / update then you can use UpsertResponse in 
ExecuteMultipleRequest  request also. 

List<CarDetails> cars = new List<CarDetails>();
cars.Add(new CarDetails(1, "X15", "BMW", 2016));
cars.Add(new CarDetails(2, "XX3", "BMW", 2014));
cars.Add(new CarDetails(3, "XX4", "BMW", 2014));
cars.Add(new CarDetails(4, "XL3", "BMW", 2014));
cars.Add(new CarDetails(1, "1Carolla CS", "Toyota", 2014));
cars.Add(new CarDetails(2, "1Carolla LE", "Toyota", 2015));
cars.Add(new CarDetails(3, "Carolla SE", "Toyota", 2015));


ExecuteMultipleRequest multipleReq = new ExecuteMultipleRequest()
 {
  Settings = new ExecuteMultipleSettings()
   {
     ContinueOnError = true,
     ReturnResponses = true
    },
   Requests = new OrganizationRequestCollection()
  };

foreach (var objcar in cars)
 {
  KeyAttributeCollection keyColl = new KeyAttributeCollection();
  keyColl.Add("new_carno", objcar.carNo);
  keyColl.Add("new_manufacture", objcar.Manufacture);

  Entity car = new Entity(new_car.EntityLogicalName, keyColl);
  car["new_name"] = objcar.CarName;
  car["new_make"] = objcar.MakeYear;

 UpsertRequest req = new UpsertRequest()
 {
   Target = car
  };

  multipleReq.Requests.Add(req);
 }


 ExecuteMultipleResponse mulresponse = (ExecuteMultipleResponse)_serviceProxy.Execute(multipleReq);


Note:  With ExecuteMultipleRequest you can execute 999 requests at a time. If you have more records than 999 in ExecuteMultipleRequest Collection, then CRM will throw error.