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

0
32
Role Image



First of all, sorry for the horrible formatting of my code. I tried for almost an hour to paste the code via the Insert in the menu. No matter which browser I use, Microsoft successfully prevents me from posting the code correctly. I need help with the following problem: I am using an Online-System and have a field on my form of the phone call entity in which the partner that is called is entered in a field (logical name: "to"). Either a Systemuser, an Account or a Contact can be entered in the field. On the form there is also a text field for the telephone number (logical name: "phonenumber"). I am trying to fill that phonenumber-field automatically when the "to" field is filled. In the form for Account and Contact the field containing the phonenumber is called "telephone1", in the case of Systemusers the field is called "address1_telephone1". In my research on how I could read another entity's field and write its value to my phonenumber-field, I came across this post: Populate Field on Form using Lookup to Another Entity My JavaScript code should be triggered when the "to" field on my PhoneCall-form is changed and currently looks like this: function To_OnChange(executionContext) { let formContext = executionContext.getFormContext(); let recipient = formContext.getAttribute("to").getValue(); if (recipient == null) { return; } var serverUrl = Xrm.Page.context.getClientUrl(); var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc/"; var type = recipient[0].entityType; switch (type) { case "account": var oDataSelect = serverUrl + ODATA_ENDPOINT + "AccountSet?$select=telephone1&$filter=AccountId eq guid'" + recipient[0].id + "'"; alert("in case account, id: " + recipient[0].id); break; case "contact": var oDataSelect = serverUrl + ODATA_ENDPOINT + "ContactSet?$select=telephone1&$filter=ContactId eq guid'" + recipient[0].id + "'"; alert("in case contact" + recipient[0].id); break; case "systemuser": var oDataSelect = serverUrl + ODATA_ENDPOINT + "SystemUserSet?$select=address1_telephone1&$filter=SystemUserId eq guid'" + recipient[0].id + "'"; alert("in case systemuser" + recipient[0].id); break; default: return; } var retrieveReq = new XMLHttpRequest(); retrieveReq.open("GET", oDataSelect, false); retrieveReq.setRequestHeader("Accept", "application/json"); retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8"); retrieveReq.onreadystatechange = function () { GetContactData(this, type); }; 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 phonenumber = null; switch (type) { case "account": case "contact": phonenumber = retrieved.results[0].telephone1; break; case "systemuser": phonenumber = retrieved.results[0].address1_telephone1; break; } Xrm.Page.getAttribute("phonenumber").setValue(phonenumber); } } } As I mentioned in the comment of the " GetContactData " function, I get a retrieveReq.status of 400 every time the script is executed. I strongly assume that my filters are incorrect. In other posts I read about a tool called "CRM REST Builder" that would help create these filters. Unfortunately, all the links I can find only lead to error pages. The alerts with the ids show me correct looking values for the IDs, the switch case is generally executed as expected. I would be very grateful if someone could help me to solve this error.



Source link

Leave a reply

Please enter your comment!
Please enter your name here