If we want to set the Primary Contact of the Account as Recipient, we need to call the following function inside the OnLoad event of the Phone Call entity.
function ChangePhoneCallRecipientFromAccountToPrimaryContact() { // check if is a new phone call if (Xrm.Page.ui.getFormType() == 1) { // get the Phone Call To Recipient var to = Xrm.Page.getAttribute("to").getValue(); // if the Recipient is an account we continue if (to != null && to[0].entityType == "account") { // get the account Id var accountId = to[0].id; // get the right url for the OData Query var serverUrl; if (Xrm.Page.context.getClientUrl !== undefined) { serverUrl = Xrm.Page.context.getClientUrl(); } else { serverUrl = Xrm.Page.context.getServerUrl(); } // build the request var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; var accountRequest = new XMLHttpRequest(); accountRequest.open("GET", ODataPath + "/AccountSet(guid'" + accountId + "')", false); accountRequest.setRequestHeader("Accept", "application/json"); accountRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8"); // execute the request accountRequest.send(); if (accountRequest.status === 200) { var retrievedAccount = JSON.parse(accountRequest.responseText).d; // retrieve the primary contact of the account var primaryContact = retrievedAccount.PrimaryContactId; // if there is a primary contact we set as new To Recipient if (primaryContact.Id != null) { var newTo = new Array(); newTo[0] = new Object(); newTo[0].id = primaryContact.Id; newTo[0].name = primaryContact.Name; newTo[0].entityType = primaryContact.LogicalName; Xrm.Page.getAttribute("to").setValue(newTo); } } else { alert("error"); } } } }
0 comments:
Post a Comment