{"id":2413,"date":"2016-02-10T16:10:25","date_gmt":"2016-02-10T10:40:25","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=2413"},"modified":"2022-07-19T16:15:30","modified_gmt":"2022-07-19T10:45:30","slug":"set-values-of-all-data-types-using-web-api-in-dynamics-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2016\/02\/set-values-of-all-data-types-using-web-api-in-dynamics-crm\/","title":{"rendered":"Set Values of all Data Types using Web API in Dynamics CRM"},"content":{"rendered":"<p style=\"text-align: justify;\">In CRM 2016, Microsoft Introduced a new concept called \u201c<strong>Web API<\/strong>\u201d (OData v4) to perform CRUD operations as well as other special messages supported in Dynamics CRM. Stepping towards the new enhancement OData v2 is deprecated from the CRM 2016.<\/p>\n<p style=\"text-align: justify;\">Though WEB API too requires the object to be sent in JSON form, it differs a little in the way the values for fields that support complex data type are set. The aim of this blog was to take up most of the common data types used and provide a sample code around how the values need to be set of each of these<\/p>\n<p><strong>Sample code to create an Account Entity Record with all Data Types<\/strong><\/p>\n<pre class=\"lang:default decode:true \">\/\/\/----------- Create with all Data Type (Account) --------\r\nfunction createAccountWithAllDT() {\r\n    try {\r\n        \/\/declare variables\r\n        var uri = null;\r\n        var stringJSONAcc = null;\r\n        var entityIdWithLink = null;\r\n        var getEntityId = null;\r\n\r\n        \/\/create JSON object \r\n        var JSONAcc = {};\r\n\r\n        \/\/set fields using JSON object\r\n        \/\/Single line of text\r\n        JSONAcc.name = \"CompanyName Pvt. Ltd.\"; \/\/Account Name\r\n        \r\n        \/\/Option Set\r\n        JSONAcc.accountcategorycode = \"2\" \/\/Category : 1--&gt; Preferred Customer, 2--&gt; Standard\r\n\r\n        \/\/Two Options\r\n        JSONAcc.donotsendmm = false;\/\/Marketing Materials : 0--&gt;False\/Send, 1--&gt;True\/Do Not Send\r\n        \/\/Whole Number\r\n        JSONAcc.numberofemployees = 151; \/\/Number of Employees\r\n\r\n        \/\/Decimal Number\r\n        JSONAcc.new_decimalnumber = 345.12; \/\/Decimal Number (Custom Field) \r\n\r\n        \/\/Lookup\r\n        JSONAcc[\"transactioncurrencyid@odata.bind\"] = \"\/transactioncurrencies(4e950855-9eb3-e511-80de-6c3be5a8ad10)\"; \/\/Currency\r\n\r\n        JSONAcc[\"primarycontactid@odata.bind\"] = \"\/contacts(DFE54660-37CD-E511-80DE-6C3BE5A831DC)\" \/\/Primary Contact\r\n\r\n        \/\/Currency\r\n        JSONAcc.creditlimit = 15000; \/\/Currency Limit\r\n\r\n        \/\/Date \r\n        JSONAcc.new_dateonly = new Date();\/\/Date Only (Custom Field)\r\n\r\n        \/\/convert JSON object to string\r\n        stringJSONAcc = JSON.stringify(JSONAcc);\r\n\r\n        \/\/url for ajax request to create account\r\n        uri = Xrm.Page.context.getClientUrl() + \"\/api\/data\/v8.0\/accounts\";\r\n\r\n        \/\/ajax request to create account\r\n        $.ajax({\r\n            type: \"POST\",\r\n            dataType: \"json\",\r\n            contentType: \"application\/json; charset=utf-8\",\r\n            url: uri,\r\n            data: stringJSONAcc,\r\n            beforeSend: function (XMLHttpRequest) {\r\n                XMLHttpRequest.setRequestHeader(\"Accept\", \"application\/json\");\r\n                XMLHttpRequest.setRequestHeader(\"Content-Type\", \"application\/json; charset=utf-8\");\r\n                XMLHttpRequest.setRequestHeader(\"Prefer\", \"odata.include-annotations=*\");\r\n            },\r\n            \/\/Success Callback Function\r\n            success: function (data, textStatus, XMLHttpRequest) {\r\n                \/\/get Response from Created Record\r\n                entityIdWithLink = XMLHttpRequest.getResponseHeader(\"OData-EntityId\");\r\n\r\n                \/\/get EntityId from ResponseHeader of Created Record  \r\n                getEntityId = entityIdWithLink.split(\/[()]\/);\r\n                getEntityId = getEntityId[1];\r\n\r\n                \/\/Display Enttity ID of Created Record\r\n                Xrm.Utility.alertDialog(\"Entity ID : \" + getEntityId);\r\n            },\r\n            \/\/Error Callback Function\r\n            error: function () {\r\n                Xrm.Utility.alertDialog(\"Something Wrong in Script POST...:(\");\r\n            }\r\n        });\r\n    } catch (e) {\r\n        Xrm.Utility.alertDialog(e.message + \"\\n\" + e.description);\r\n    }\r\n}<\/pre>\n<p><strong>Sample code to create an Activity &amp; set Activity Party<\/strong><\/p>\n<pre class=\"lang:default decode:true \">\/\/\/------------ Create Phone Call Activity ----------\r\n\r\nfunction createPhoneCall() {\r\n    try {\r\n        \/\/declare variables\r\n        var phonecallId = null;\r\n        var stringJSONPhone = null;\r\n        var urlPhone = null;\r\n\r\n        \/\/create activity party collection\r\n        var parties = [];\r\n\r\n        \/\/create JSON object \r\n        var JSONPhone = {};\r\n\r\n        \/\/set fields using JSON object\r\n        \/\/Single line of text\r\n        JSONPhone[\"subject\"] = \"Test Phone Call\"; \/\/Subject\r\n\r\n        \/\/Single line of text &amp; format of phone \r\n        JSONPhone[\"phonenumber\"] = \"9876543210\"; \/\/Phone Number\r\n\r\n        \/\/Multiple Line of Text\r\n        JSONPhone[\"description\"] = \"Phone Call Activity for Testing Purpose only...!\"; \/\/Description\r\n\r\n        \/\/Date and Time\r\n        JSONPhone[\"scheduledend\"] = new Date(); \/\/Due\r\n\r\n        \/\/Lookup\r\n        JSONPhone[\"regardingobjectid_account@odata.bind\"] = \"\/accounts(B386D403-F7AD-E511-80DC-A45D36FC4F90)\"; \/\/Regarding is an account\r\n\r\n        \/\/ActivityParty (From)\r\n        var sender = {};\r\n        sender[\"partyid_systemuser@odata.bind\"] = \"\/systemusers(D949B11D-9240-4037-8379-F31C7A36680E)\";\r\n        sender[\"participationtypemask\"] = 1; \/\/From\r\n\r\n        \/\/ActivityParty (To)\r\n        var receiver1 = {};\r\n        receiver1[\"partyid_account@odata.bind\"] = \"\/accounts(B386D403-F7AD-E511-80DC-A45D36FC4F90)\";\r\n        receiver1[\"participationtypemask\"] = 2; \/\/To\r\n        \/\/receiver[\"addressused\"] = \"roohi@dyn20161.onmicrosoft.com\";\r\n\r\n        var receiver2 = {};\r\n        receiver2[\"partyid_contact@odata.bind\"] = \"\/contacts(DFE54660-37CD-E511-80DE-6C3BE5A831DC)\";\r\n        receiver2[\"participationtypemask\"] = 2; \/\/To\r\n\r\n        var receiver3 = {};\r\n        receiver3[\"partyid_lead@odata.bind\"] = \"\/leads(ED81F0D9-37CD-E511-80DE-6C3BE5A831DC)\";\r\n        receiver3[\"participationtypemask\"] = 2; \/\/To\r\n\r\n        \/\/add this to collection\r\n        parties.push(sender);\r\n        parties.push(receiver1);\r\n        parties.push(receiver2);\r\n        parties.push(receiver3);\r\n\r\n        \/\/pass parties[] to phonecall_activity_parties\r\n        JSONPhone[\"phonecall_activity_parties\"] = parties;\r\n\r\n        \/\/Whole Number\r\n        JSONPhone[\"actualdurationminutes\"] = 25; \/\/Duration\r\n\r\n        \/\/Two Options\r\n        JSONPhone[\"directioncode\"] = true;\/\/Direction : 0--&gt;False\/Incomming, 1--&gt;True\/Outgoing\r\n\r\n        \/\/convert JSON object to string\r\n        stringJSONPhone = JSON.stringify(JSONPhone);\r\n\r\n        \/\/url for ajax request to create phonecall activity\r\n        urlPhone = Xrm.Page.context.getClientUrl() + \"\/api\/data\/v8.0\/phonecalls\";\r\n\r\n        \/\/ajax request to create phonecall activity\r\n        $.ajax({\r\n            type: \"POST\",\r\n            contentType: \"application\/json; charset=utf-8\",\r\n            dataType: \"json\",\r\n            url: urlPhone,\r\n            data: stringJSONPhone,\r\n            beforeSend: function (CreatePhoneCallActivityRequest) {\r\n                CreatePhoneCallActivityRequest.setRequestHeader(\"Accept\", \"application\/json\");\r\n                CreatePhoneCallActivityRequest.setRequestHeader(\"Content-Type\", \"application\/json; charset=utf-8\");\r\n                CreatePhoneCallActivityRequest.setRequestHeader(\"Prefer\", \"odata.include-annotations=*\");\r\n                CreatePhoneCallActivityRequest.setRequestHeader(\"OData-MaxVersion\", \"4.0\");\r\n                CreatePhoneCallActivityRequest.setRequestHeader(\"OData-Version\", \"4.0\");\r\n            },\r\n            \/\/Success Callback Function\r\n            success: function (data, taxtStatus, getPhoneCallActivityResponse) {\r\n                \/\/get Response from Created Record\r\n                phonecallId = getPhoneCallActivityResponse.getResponseHeader(\"OData-EntityId\");\r\n\r\n                \/\/get EntityId from ResponseHeader of Created Record \r\n                phonecallId = phonecallId.split(\/[()]\/);\r\n                phonecallId = phonecallId[1];\r\n\r\n                \/\/Display Enttity ID of Created Record\r\n                Xrm.Utility.alertDialog(\"Entity ID : \" + phonecallId);\r\n\r\n            },\r\n            \/\/Error Callback Function\r\n            error: function (CreatePhoneCallActivityRequest, textStatus, errorThrown) {\r\n                Xrm.Utility.alertDialog(\"Something Wrong in Script...:(\");\r\n            }\r\n        });\r\n    } catch (e) {\r\n        Xrm.Utility.alertDialog(e.message + \"\\n\" + e.description);\r\n    }\r\n}<\/pre>\n<p><strong>Conclusion:<\/strong><\/p>\n<p style=\"text-align: justify;\">Lookup, Regarding and Activity parties being special complex field need to be set by providing a reference to the entity type that you intend to set. The field name also includes a reference to the entity as can be seen in the sample code above. This differs from the way it used to be done earlier where entity type used to be provided as a property of the Lookup object instead of in the field name itself.<\/p>\n<h2 style=\"text-align: left;\"><div class=\"su-heading su-heading-style-default su-heading-align-center\" id=\"\" style=\"font-size:15px;margin-bottom:5px\"><div class=\"su-heading-inner\">70% of global 2000 companies apply gamification to improve productivity and returns!<\/div><\/div><\/h2>\n<p><em><strong><a href=\"https:\/\/bit.ly\/3RD4lYW\" target=\"_blank\" rel=\"noopener noreferrer\">Gamifics365<\/a> <\/strong>\u2013 Spin the magic of games within Microsoft Dynamics 365 CRM to improve user adoption, enhance productivity, and achieve company goals!<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In CRM 2016, Microsoft Introduced a new concept called \u201cWeb API\u201d (OData v4) to perform CRUD operations as well as other special messages supported in Dynamics CRM. Stepping towards the new enhancement OData v2 is deprecated from the CRM 2016. Though WEB API too requires the object to be sent in JSON form, it differs\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2016\/02\/set-values-of-all-data-types-using-web-api-in-dynamics-crm\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[19,24,65],"tags":[1817,1825,1826],"class_list":["post-2413","post","type-post","status-publish","format-standard","hentry","category-dynamics-crm","category-dynamics-crm-2016","category-webapi","tag-web-api-dynamics-crm","tag-web-api-in-dynamics-crm","tag-web-api-ms-dynamics-crm"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/2413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=2413"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/2413\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=2413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=2413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=2413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}