{"id":1665,"date":"2015-09-30T11:12:05","date_gmt":"2015-09-30T11:12:05","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=1665"},"modified":"2015-11-30T10:33:42","modified_gmt":"2015-11-30T10:33:42","slug":"validate-inactive-customer-selection","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=1665","title":{"rendered":"Validate Inactive Customer Selection"},"content":{"rendered":"<p><strong>Requirement:<\/strong> Let&#8217;s say we want to validate customer selection in opportunity record and if user selected inactive record, we want to display them an error alert to select another customer.<\/p>\n<p><strong>Solution:<\/strong> We can implement this requirement using server side or client side code, in this post we are going to provide sample code for checking account status and throw alert in case of inactive customer selected. We can create a Java Script web resource and can use below code. We can call our <strong>CheckIfAccountIsInactive<\/strong> method on <strong>onchange<\/strong> of <strong>parentaccountid<\/strong> field in opportunity form.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction CheckIfAccountIsInactive() {\r\n    if (Xrm.Page.getAttribute('parentaccountid').getValue() != null) {\r\n        var AccountID = Xrm.Page.getAttribute('parentaccountid').getValue()[0].id;\r\n        var ClientURL = Xrm.Page.context.getClientUrl() + '\/XRMServices\/2011\/OrganizationData.svc';\r\n        var Filter = '\/AccountSet?$filter=AccountId eq guid'' + AccountID + ''';\r\n        var Req = new XMLHttpRequest();\r\n        Req.open('GET', ClientURL + Filter, false);\r\n        Req.setRequestHeader('Accept', 'application\/json');\r\n        Req.setRequestHeader('Content-Type', 'application\/json; charset=utf-8');\r\n        Req.send();\r\n        if (Req.readyState == 4 \/* complete *\/ ) {\r\n            if (Req.status == 200) {\r\n                var retrieved = this.JSON.parse(Req.responseText).d;\r\n                var accountState = retrieved.results[0].StateCode.Value;\r\n                if (accountState == 1) {\r\n                    alert('This Customer is Inactive, Please select another customer');\r\n                    Xrm.Page.getAttribute('parentaccountid').setValue(null);\r\n                    return;\r\n                }\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Once we have published our changes, if we will try to select inactive account in opportunity record\u00a0we will get alert like below:<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/09\/validatecustomer.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-1667 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/09\/validatecustomer-300x224.png\" alt=\"validatecustomer\" width=\"300\" height=\"224\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/09\/validatecustomer-300x224.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/09\/validatecustomer.png 551w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Requirement: Let&#8217;s say we want to validate customer selection in opportunity record and if user selected inactive record, we want to display them an error alert to select another customer. Solution: We can implement this requirement using server side or client side code, in this post we are going to provide sample code for checking account status and throw alert&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=1665\">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,6,8],"tags":[216,218,217],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1665"}],"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=1665"}],"version-history":[{"count":7,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1665\/revisions"}],"predecessor-version":[{"id":1894,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1665\/revisions\/1894"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}