Forum Post: RE: Transfer Value from Field of on Entity to another using REST leads to Error 400 (BadRequest)

0
23
Role Image



Hi, I have modified your code to use web api 9.1 and it works as expected. function To_OnChange(executionContext) { let formContext = executionContext.getFormContext(); let recipient = formContext.getAttribute("to").getValue(); if (recipient == null || recipient.length==0) { return; } var serverUrl = Xrm.Page.context.getClientUrl(); var ODATA_ENDPOINT = "/api/data/v9.1/"; var oDataSelect=""; var type = recipient[0].entityType; switch (type) { case "account": oDataSelect = serverUrl + ODATA_ENDPOINT + "accounts("+ recipient[0].id.replace('{','').replace('}','') +")?$select=telephone1"; alert("in case account, id: " + recipient[0].id); break; case "contact": oDataSelect = serverUrl + ODATA_ENDPOINT + "contacts("+ recipient[0].id.replace('{','').replace('}','') +")?$select=telephone1"; alert("in case contact" + recipient[0].id); break; case "systemuser": oDataSelect = serverUrl + ODATA_ENDPOINT + "systemusers("+ recipient[0].id.replace('{','').replace('}','') +")?$select=address1_telephone1"; alert("in case systemuser" + recipient[0].id); break; default: return; } var retrieveReq = new XMLHttpRequest(); retrieveReq.open("GET", oDataSelect, false); retrieveReq.setRequestHeader("OData-MaxVersion", "4.0"); retrieveReq.setRequestHeader("OData-Version", "4.0"); retrieveReq.setRequestHeader("Accept", "application/json"); retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); retrieveReq.setRequestHeader("Prefer", "odata.include-annotations="*""); retrieveReq.onreadystatechange = function () { if (this.readyState === 4) { retrieveReq.onreadystatechange = null; alert("retrieveReq.status: " + retrieveReq.status); if (this.status === 200) { var result = JSON.parse(this.response); var phonenumber = null; switch (type) { case "account": case "contact": phonenumber = result["telephone1"]; break; case "systemuser": phonenumber = result["address1_telephone1"]; break; } alert("Phone Number"+phonenumber); Xrm.Page.getAttribute("phonenumber").setValue(phonenumber); } else { Xrm.Utility.alertDialog(this.statusText); } } }; retrieveReq.send(); } function GetContactData(retrieveReq, type) { if (retrieveReq.readyState == 4) { alert("retrieveReq.status: " + retrieveReq.status); if (retrieveReq.status == 200) { // 200 = retrieve OK – this is never executed because the status is always 400 (BadRequest Expect this when an argument is invalid.) //var retrieved = JSON.parse(retrieveReq.responseText).d; var retrieved = JSON.parse(this.response); var phonenumber = null; switch (type) { case "account": case "contact": phonenumber = result["telephone1"]; break; case "systemuser": phonenumber = result["address1_telephone1"]; break; } Xrm.Page.getAttribute("phonenumber").setValue(phonenumber); } } } Please mark my answer verified if this is helpful! Regards, Bipin Kumar Follow my Blog: xrmdynamicscrm.wordpress.com/



Source link

Leave a reply

Please enter your comment!
Please enter your name here