{"id":2874,"date":"2017-11-21T16:09:40","date_gmt":"2017-11-21T16:09:40","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=2874"},"modified":"2018-08-09T06:01:34","modified_gmt":"2018-08-09T06:01:34","slug":"implementing-field-mapping-using-xrm-webapi","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=2874","title":{"rendered":"Implementing field mapping using Xrm.WebApi"},"content":{"rendered":"<p>In our last article we discussed <a href=\"https:\/\/himbap.com\/blog\/?p=2852\">Xrm.WebApi create, update and delete method<\/a>. In this article we are going to provide sample code for using Xrm.WebApi retrieve method.<\/p>\n<p>Many times we required to setup field mapping between parent and child entities, for example let\u2019s say we need to bring following information based on the account selected under contact entity. We can easily set these fields mapping by editing account and contact relationship. But that relationship mapping will only work when contact record is created from account entity form.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/fielmapping1.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2875 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/fielmapping1-300x252.png\" alt=\"fielmapping1\" width=\"300\" height=\"252\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/fielmapping1-300x252.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/fielmapping1.png 431w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nBut if we are creating contact record directly relationship mapping won\u2019t work here. So in this article we are providing sample code to implement above mapping using retrieve method.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/retrieve data based on primary entity id\r\nfunction retrieveAccountDetails() {\r\n  \/\/read lookup value\r\n  if (Xrm.Page.getAttribute(&quot;parentcustomerid&quot;).getValue() != null &amp;&amp; Xrm.Page.getAttribute(&quot;parentcustomerid&quot;).getValue()[0].id != null) {\r\n    var accountid = Xrm.Page.getAttribute(&quot;parentcustomerid&quot;).getValue()[0].id;\r\n\r\n    \/\/pass entity, fields, we can use expand to get related entity fields\r\n    Xrm.WebApi.retrieveRecord(&quot;account&quot;, accountid, &quot;?$select=telephone1,new_verificationrequired,new_activationdate,address1_shippingmethodcode&amp;$expand=parentaccountid($select=accountid,name)&quot;).then(\r\n      function success(result) {\r\n        if (result != null) {\r\n          \/\/set text field\r\n          if (result.telephone1 != null)\r\n            Xrm.Page.getAttribute(&quot;telephone1&quot;).setValue(result.telephone1);\r\n          \/\/set lookup field\r\n          if (result.parentaccountid != null) {\r\n            var object = new Array();\r\n            object[0] = new Object();\r\n            object[0].id = result.parentaccountid.id;\r\n            object[0].name = result.parentaccountid.name;\r\n            object[0].entityType = &quot;account&quot;;\r\n            Xrm.Page.getAttribute(&quot;new_parentaccount&quot;).setValue(object);\r\n          }\r\n          \/\/set two optionset\r\n          if (result.new_verificationrequired != null)\r\n            Xrm.Page.getAttribute(&quot;new_verificationrequired&quot;).setValue(result.new_verificationrequired);\r\n          \/\/set date field\r\n          if (result.new_activationdate != null)\r\n            Xrm.Page.getAttribute(&quot;new_activationdate&quot;).setValue(new Date(result[&quot;new_activationdate@OData.Community.Display.V1.FormattedValue&quot;]));\r\n          \/\/set optionset field\r\n          if (result.address1_shippingmethodcode != null)\r\n            Xrm.Page.getAttribute(&quot;address1_shippingmethodcode&quot;).setValue(result.address1_shippingmethodcode);\r\n        }\r\n      },\r\n      function(error) {\r\n        alert(error.message);\r\n\r\n      }\r\n    );\r\n  }\r\n}\r\n<\/pre>\n<p>Above code can be modified based on required fields that we want to fetch from the parent entity and can call above function on onChange of the lookup field for parent entity, for example in above scenario it can be used on onchange of parentcustomerid lookup field.<\/p>\n<p>Stay Tuned for more\u00a0Dynamics 365 Sample code\u00a0!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last article we discussed Xrm.WebApi create, update and delete method. In this article we are going to provide sample code for using Xrm.WebApi retrieve method. Many times we required to setup field mapping between parent and child entities, for example let\u2019s say we need to bring following information based on the account selected under contact entity. We can&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=2874\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[402],"tags":[483,484,486,485,482],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2874"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2874"}],"version-history":[{"count":11,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2874\/revisions"}],"predecessor-version":[{"id":3173,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2874\/revisions\/3173"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}