{"id":861,"date":"2012-11-20T09:21:51","date_gmt":"2012-11-20T09:21:51","guid":{"rendered":"http:\/\/mahenderpal.wordpress.com\/?p=861"},"modified":"2012-11-20T09:21:51","modified_gmt":"2012-11-20T09:21:51","slug":"are-you-sure-you-want-to-change-lookup-value","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=861","title":{"rendered":"Are you sure you want to change lookup value ??"},"content":{"rendered":"<p>Sometime we have requirement where we need to prompt user while changing lookup value if he really wants change that value or not, especially when we need to fire some business logic on change of lookup based on it\u2019s value. We got this requirement where we need to delete all child records when specific lookup in parent entity is changed, so in order eliminate the possibilities to change lookup value by mistake, we implemented below logic<\/p>\n<p>if (Xrm.Page.ui.getFormType() == 2) {<br \/>\nif (Xrm.Page.getAttribute(&#8220;lookupField&#8221;).getValue() != null) {<br \/>\nvar LookupID = Xrm.Page.getAttribute(&#8220;lookupField&#8221;).getValue()[0].id;<br \/>\nvar EntityID = Xrm.Page.data.entity.getId();<br \/>\nvar context = Xrm.Page.context;<br \/>\nvar serverUrl = context.getServerUrl();<br \/>\nvar ODataPath = serverUrl + &#8220;\/XRMServices\/2011\/OrganizationData.svc&#8221;;<br \/>\nvar retrieveResult = new XMLHttpRequest();<br \/>\nretrieveResult.open(&#8220;GET&#8221;, ODataPath + &#8220;\/EntityNameSet(guid'&#8221; + EntityID + &#8220;&#8216;)&#8221;, false); \/\/query entity based on it&#8217;s id<br \/>\nretrieveResult.setRequestHeader(&#8220;Accept&#8221;, &#8220;application\/json&#8221;);<br \/>\nretrieveResult.setRequestHeader(&#8220;Content-Type&#8221;, &#8220;application\/json; charset=utf-8&#8221;);<br \/>\nretrieveResult.send();<br \/>\nif (retrieveResult.readyState == 4 \/* complete *\/) {<br \/>\nif (retrieveResult.status == 200) {<br \/>\nvar retrievedCard = this.parent.JSON.parse(retrieveResult.responseText).d;<br \/>\n\/\/check if lookup value is different then existing lookup value in database<br \/>\nif (retrievedCard.lookupField != null) {<br \/>\nif (retrievedCard.lookupField.Id != LookupID) {<br \/>\nvar Response = confirm(&#8220;Are you sure you want to change lookup value&#8221;); \/\/prompt message<br \/>\nif (Response != true) {<br \/>\n\/\/set lookup with last value<br \/>\nvar lookup = new Object();<br \/>\nvar lookupValue = new Array();<br \/>\nlookup.id = retrievedCard.lookupField.Id;<br \/>\nlookup.entityType = retrievedCard.lookupField.Id.EntityType;<br \/>\nlookup.name = retrievedCard.lookupField.Id.Name;<br \/>\nlookupValue[0] = lookup;<br \/>\nXrm.Page.getAttribute(&#8220;lookupField&#8221;).setValue(lookupValue);<br \/>\n}<br \/>\n} } } }}}<\/p>\n<p>Hope it will help somebody to implement similar kind of requirement.<\/p>\n<p>Enjoy !!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometime we have requirement where we need to prompt user while changing lookup value if he really wants change that value or not, especially when we need to fire some business logic on change of lookup based on it\u2019s value. We got this requirement where we need to delete all child records when specific lookup in parent entity is changed,&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=861\">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":[5],"tags":[111,149,170],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/861"}],"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=861"}],"version-history":[{"count":0,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/861\/revisions"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}