I'm using JS to open a new Phone Call record on a button click from a Contact record. My code takes the user and contact info to prepopulate the new Phone Call record. Everything works great except I get the following duplicated errors each time it's called:
These errors do not appear if I comment out the openForm function in my code. Somehow, when this function executes, it is calling twice a retrieveRecord function to get a contacts email address using my User GUID, hence the error.
I've tried using breakpoints but the code doesn't pause! I had to add a break point at the beginning of the code to get it to pause and then add breakpoints in throughout the rest of the code - but when I do that the error doesn't appear!
This doesn't break my code's function but I'd really like to not make random API calls or have unnecessary errors popping up.
Here's my code in question:
function makeCall() { Xrm.Utility.showProgressIndicator("Connecting..."); var userName = Xrm.Utility.getGlobalContext().userSettings.userName; var userId = Xrm.Utility.getGlobalContext().userSettings.userId; userId = userId.substring(1, userId.length - 1); var contactName = window.parentFormContext.data.entity.getPrimaryAttributeValue(); var contactId = window.parentFormContext.data.entity.getId(); contactId = contactId.substring(1,contactId.length-1); Xrm.WebApi.retrieveRecord("systemuser", userId, "?$select=mobilephone").then( function success(result) { var userPhone = result.mobilephone.replace(/\(|\)|\-|\ |\./g, ""); var contactPhone = window.parentFormContext.getAttribute("telephone1").getValue().replace(/\(|\)|\-|\ |\./g, ""); var body = { "Url": "https://handler.twilio.com/twiml/####?contactPhone=" contactPhone, "To": userPhone, "From": " 13219999290" }; body = JSON.stringify(body); var url = "https://####"; var req = new XMLHttpRequest(); req.open("POST", url, true); req.setRequestHeader('Content-Type', 'application/json'); req.send(body); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { Xrm.Utility.closeProgressIndicator(); var entityFormOptions = {}; entityFormOptions["entityName"] = "phonecall"; var formParameters = {}; formParameters["subject"] = "RFI Follow-Up Call"; formParameters["regardingobjectid"] = contactId; formParameters["regardingobjectidname"] = contactName; formParameters["regardingobjectidtype"] = "contact"; var to = new Array(); to[0] = new Object(); to[0].id = contactId; to[0].name = contactName; to[0].entityType = 'contact'; formParameters['to'] = to; var from = new Array(); from[0] = new Object(); from[0].id = userId; from[0].name = userName from[0].entityType = 'contact'; formParameters['from'] = from; Xrm.Navigation.openForm(entityFormOptions, formParameters).then( function (success) { console.log(success); }, function (error) { console.log(error); }); } else if (this.status === 400) { Xrm.Utility.closeProgressIndicator(); var result = "ERROR: Please contact your CRM administrator."; Xrm.Navigation.openErrorDialog({ message: result }); } } } }, function (error) { Xrm.Utility.closeProgressIndicator(); Xrm.Navigation.openErrorDialog({ message: error.message }); } ) }