{New Feature} Get the created object after creating a record using Web API in Dynamics 365

Another blog post and with it another great feature of Web API enhancements in Dynamics 365 that I will be sharing it. A small enhancement but a really handy one I would say.

So let’s understand what this feature I am talking about. If you just saw the header and jumped into this post without really understanding much from the title of this topic, don’t be disappointed. It’s my bad and I am terrible to in finding click bait headers for my topics. Hope I will do a decent job explaining the feature. So buckle up.

Prior to Dynamics 365, when you create a record in Dynamics CRM, be it through Web API or through Organization Service, you would get the guid of the newly created record back. And to get the whole object back you would again need to query the with the returned guid.

The problem become more prominent if you are using the deep insert feature using Web API. For example when I create a account, I can create an associated contact in the same operation. However after the create operation, all you get back is the guid of account record.

Well if you have been bothered by this, no need to worry anymore. The awesome Dynamics 365 is here to bail you out.

With Dynamics 365, you can pass an optional parameter in the header of the request to return the created record if you wish to. In the below example, I am creating an account record and an associated contact record as well and then returning the whole account record

function createAccount() {
    debugger;

    var serverURL = Xrm.Page.context.getClientUrl();
    var account = {};
    account["name"] = "Web API representation example";
    account["address1_city"] = "Bangalore";

    account["primarycontactid"] = {
        "firstname": "Debajit",
        "lastname": "Dutta"
    };

    account["numberofemployees"] = 1000;

    var req = new XMLHttpRequest();
    req.open("POST", serverURL + "/api/data/v8.2/accounts", false);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Prefer", "return=representation");
  
    req.send(JSON.stringify(account));
   
    var response = req.responseText;
}

The key in the above code is the line highlighted. Infact if you do not pass the above, the request internally sends a default header of return=minimal and that’s why all we get back is the guid of the created record.

Below is the JSON result I get back:

 

"{

  "@odata.context":"https://xrmtr30.crm.dynamics.com/api/data/v8.2/$metadata#accounts/$entity","@odata.etag":"W/\"642226\"","openrevenue_date":"2016-11-02T15:02:27Z","territorycode":1,"address2_freighttermscode":1,"address2_shippingmethodcode":1,"accountid":"785a73fc-73a1-e611-80e6-c4346bac4500","statuscode":1,"creditonhold":false,"donotbulkemail":false,"donotsendmm":false,"createdon":"2016-11-03T03:17:02Z","statecode":0,"businesstypecode":1,"donotpostalmail":false,"_ownerid_value":"e4306322-8b0f-4cb4-93e9-1d2da3169fdd","donotbulkpostalmail":false,"name":"Web API representation example","address1_city":"Bangalore","donotemail":false,"donotphone":false,"address1_composite":"Bangalore","address2_addresstypecode":1,"versionnumber":642226,"opendeals_date":"2016-11-02T15:02:27Z","_modifiedby_value":"e4306322-8b0f-4cb4-93e9-1d2da3169fdd","shippingmethodcode":1,"followemail":true,"preferredcontactmethodcode":1,"numberofemployees":1000,"_owningbusinessunit_value":"e8d2b7eb-199e-e611-80ea-c4346bacda3c","_primarycontactid_value":"795a73fc-73a1-e611-80e6-c4346bac4500","_createdby_value":"e4306322-8b0f-4cb4-93e9-1d2da3169fdd","donotfax":false,"merged":false,"customersizecode":1,"marketingonly":false,"accountratingcode":1,"address2_addressid":"e836a8b7-c4f7-44b4-b46e-344c3d312622","_owninguser_value":"e4306322-8b0f-4cb4-93e9-1d2da3169fdd","modifiedon":"2016-11-03T03:17:02Z","participatesinworkflow":false,"accountclassificationcode":1,"address1_addressid":"13b1f69c-6b06-4990-be7e-2ab502b71b19","openrevenue":null,"lastusedincampaign":null,"address1_name":null,"address1_telephone2":null,"overriddencreatedon":null,"entityimageid":null,"ownershipcode":null,"creditlimit":null,"address1_county":null,"entityimage_timestamp":null,"telephone3":null,"address1_freighttermscode":null,"address2_line1":null,"onholdtime":null,"int_twitter":null,"_createdbyexternalparty_value":null,"telephone2":null,"primarysatoriid":null,"address2_telephone1":null,"address1_fax":null,"_createdonbehalfby_value":null,"address2_city":null,"address2_latitude":null,"address2_composite":null,"creditlimit_base":null,"aging90_base":null,"address2_postalcode":null,"address2_name":null,"preferredappointmentdaycode":null,"_owningteam_value":null,"address2_line3":null,"revenue_base":null,"aging30":null,"ftpsiteurl":null,"_originatingleadid_value":null,"emailaddress3":null,"_modifiedbyexternalparty_value":null,"paymenttermscode":null,"websiteurl":null,"revenue":null,"address1_shippingmethodcode":null,"description":null,"tickersymbol":null,"lastonholdtime":null,"marketcap":null,"_parentaccountid_value":null,"_preferredsystemuserid_value":null,"address2_longitude":null,"_defaultpricelevelid_value":null,"openrevenue_state":null,"address1_primarycontactname":null,"accountcategorycode":null,"marketcap_base":null,"address2_telephone3":null,"address2_postofficebox":null,"_preferredserviceid_value":null,"importsequencenumber":null,"address1_upszone":null,"stageid":null,"utcconversiontimezonecode":null,"aging60":null,"customertypecode":null,"telephone1":null,"opendeals_state":null,"address1_postofficebox":null,"yominame":null,"address2_country":null,"address1_line2":null,"address1_postalcode":null,"traversedpath":null,"_transactioncurrencyid_value":null,"fax":null,"address2_upszone":null,"aging90":null,"address1_longitude":null,"emailaddress2":null,"address1_telephone1":null,"exchangerate":null,"address1_stateorprovince":null,"address2_line2":null,"entityimage":null,"primarytwitterid":null,"timespentbymeonemailandmeetings":null,"accountnumber":null,"address1_line1":null,"_territoryid_value":null,"_slaid_value":null,"address2_county":null,"_preferredequipmentid_value":null,"sic":null,"entityimage_url":null,"processid":null,"address2_telephone2":null,"address1_addresstypecode":null,"address1_utcoffset":null,"address2_fax":null,"aging60_base":null,"address1_country":null,"_modifiedonbehalfby_value":null,"stockexchange":null,"address1_line3":null,"int_facebook":null,"aging30_base":null,"openrevenue_base":null,"sharesoutstanding":null,"_slainvokedid_value":null,"address2_primarycontactname":null,"address1_latitude":null,"_masterid_value":null,"address2_utcoffset":null,"emailaddress1":null,"timezoneruleversionnumber":null,"opendeals":null,"address2_stateorprovince":null,"preferredappointmenttimecode":null,"industrycode":null,"address1_telephone3":null

}"

As you can see, not only I get the account record back but also the GUID of the primary contact associated with the entity record that I created using deep insert.

Please note that the status of the response would be 201 (Created).

Stay tuned for another wonderful update from the world of Dynamics CRM

 

Debajit Dutta

(Visit http://www.xrmforyou.com/products.html and checkout our tools library)

Advertisements

About Debajit
I am a Dynamics CRM Most Valuable Professional (MVP) with 10 years of experience in Microsoft .NET Technologies and 7 years of dedicated experience in Microsoft Dynamics CRM. I have worked with companies like Microsoft, SanDisk, PwC, TMF Group and have extensive experience of implementing complex CRM solutions from both offshore and client side. Currently the face of XrmForYou.com with significant experience in delivering corporate training on Dynamics CRM and have already delivered multiple projects to client through XrmForYou.com Author of multiple tools on codeplex including the 'Role Based Views' and 'CRM-Sharepoint Metadata manager & Attachment Extractor' which are available for commercial use under XrmForYou.com For consulting/ training, drop me a note at info@xrmforyou.com or visit our website www.xrmforyou.com

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

%d bloggers like this: