{Dynamics CRM + Upsert} Execute Upsert request from client side in Dynamics CRM

Upsert request was introduced in Dynamics CRM 2015 Online Update 1 and on-premise customers got a flavour of it from CRM 2016 onwards. After this feature has been introduced, it has been used so much that writing anything on it is now hackneyed.

However just when you feel you have done it 100 times, you may still find that one scenario suddenly popping up which you might have not implemented in your previous projects and this was exactly one of those. My client wanted to fire an Upsert request from the client side. Basically the requirement was, there was a webresource where the user would fill the necessary information for a record and based on the field values entered, an Upsert request would create/ update the record accordingly.

So what do I need here? Well the first thing I need here is the XML request. And how do I get that?  Did you forget about the wonderful SOAP Logger tool that ships with SDK. Yeah and it still works great. Smile

By any chance you are not aware where you can find the SOAP Logger, you can find in your SDK Download in the following folder structure – SDK\SampleCode\CS\Client\SOAPLogger

Let’s take an example here. For account entity, first I create an alternate key with combination of Account Number + Account Name.


I open the SOAP logger and run the below code to get the corresponding XML request for Upsert request

SoapLoggerOrganizationService slos = new SoapLoggerOrganizationService(serverConfig.OrganizationUri, service, output);

                              //Add the code you want to test here:
                              // You must use the SoapLoggerOrganizationService ‘slos’ proxy rather than the IOrganizationService proxy you would normally use.

                              KeyAttributeCollection acckeys = new KeyAttributeCollection();

                              acckeys.Add("accountnumber", "ASH001");
                              acckeys.Add("name", "SOAP Logger for Test");
                              Entity account = new Entity("account", acckeys);
                              account["name"] = "SOAP Logger for Test";

                              UpsertRequest upsert = new UpsertRequest();
                              upsert.Target = account;
                              UpsertResponse response = (UpsertResponse)slos.Execute(upsert);


Once I run the above code, the below is the XML Request

<s:Envelope xmlns:s="">
    <Execute xmlns="" xmlns:i="">
      <request i:type="a:UpsertRequest" xmlns:a="">
        <a:Parameters xmlns:b="">
            <b:value i:type="a:Entity">
                  <b:value i:type="c:string" xmlns:c="">SOAP Logger for Test</b:value>
              <a:EntityState i:nil="true" />
              <a:FormattedValues />
             <a:KeyAttributes xmlns:c="">
                  <b:value i:type="d:string" xmlns:d="">ASH001</b:value>
                  <b:value i:type="d:string" xmlns:d="">SOAP Logger for Test</b:value>
              <a:RelatedEntities />
              <a:RowVersion i:nil="true" />
        <a:RequestId i:nil="true" />

This request is just like any other request for Create/ Update except for the highlighted part. If you notice carefully, you could find a new tag in XML request name KeyAttributes. All you need to do is replace the values of the KeyAttributes

Here is the sample code for the same in javascript

var accountNumber = ‘ASH001’;
var accountName = ‘SOAP Logger for Test’;

var request = [‘<s:Envelope xmlns:s="">’,
    ‘<Execute xmlns="" xmlns:i="">’,
      ‘<request i:type="a:UpsertRequest" xmlns:a="">’,
        ‘<a:Parameters xmlns:b="">’,
            ‘<b:value i:type="a:Entity">’,
                  ‘<b:value i:type="c:string" xmlns:c="">’, accountName, ‘</b:value>’,
              ‘<a:EntityState i:nil="true" />’,
              ‘<a:FormattedValues />’,
              ‘<a:KeyAttributes xmlns:c="">’,
                  ‘<b:value i:type="d:string" xmlns:d="">’, accountNumber,'</b:value>’,
                  ‘<b:value i:type="d:string" xmlns:d="">’, accountName, ‘</b:value>’,
              ‘<a:RelatedEntities />’,
              ‘<a:RowVersion i:nil="true" />’,
        ‘<a:RequestId i:nil="true" />’,

var req = new XMLHttpRequest();"POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", false);
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");




The below is the response text.

<s:Envelope xmlns:s="">
    <ExecuteResponse xmlns="">
      <ExecuteResult i:type="a:UpsertResponse" xmlns:a="" xmlns:i="">
        <a:Results xmlns:b="">
            <b:value i:type="c:boolean" xmlns:c="">false</b:value>
            <b:value i:type="a:EntityReference">
              <a:KeyAttributes xmlns:c=""/>
              <a:Name i:nil="true"/>
              <a:RowVersion i:nil="true"/>


In the response, you can find whether the record is created or updated. Please see the highlighted section. I already have an account with same accountnumber and name and hence RecordCreated value has been set to false


Hope this helps!