I'm trying to add some JavaScript to the "to" field's onChange event of the Phone Call activity.
I would like to grab the first entry in the "to" field and determine if it is an Account or Contact for the purpose of getting some data from the respective record.
It appears I cannot get the "type". In line 7 below my alert shows "undefined". If I change the alert to "members[0].id" then the GUID displays. So I know it is getting there. But "type" doesn't work. And the subsequent IF statements never work. I am choosing an account or contact while testing.
Is there something I am missing here? I've researched online and other people have posted that "members[i].type" is the correct way to get the entity type.
Thank you in advance for any help you may have.
John.
function getparty(executionContext) {
var formContext = executionContext.getFormContext();
var party = formContext.getAttribute("to");
var members = party.getValue();
alert(members[0].type);
// Contact
if (members[0].type == 2) {
alert("Contact Id-" + members[0].id + " Name-" + members[0].name);
//Entity name
var sEntityName = "contact";
// Select field names:
var sColumns = "telephone1";
// Where clause filter
var sFilter = "contactid eq " + members[0].id;
var sOrderBy = "";
//Call the retrieve function
var requestResults = retrieveWebAPIData(sEntityName, sColumns, sFilter, sOrderBy);
//Loop through the results (there should only be one in this case):
if (requestResults != null) {
if (requestResults.value != null && requestResults.value.length > 0) {
for (var indRecordLoop = 0; indRecordLoop < requestResults.value.length; indRecordLoop++) {
var oContact = requestResults.value[indRecordLoop];
//Check returned data field for a value:
if(oContact.telephone1)
{
Xrm.Page.getAttribute("phonenumber").setValue(oContact.telephone1);
}
}
}
}
}
// Account
if (members[0].type == 1) {
alert("Account Id-" + members[0].id + " Name-" + members[0].name);
//Entity name
var sEntityName = "account";
// Select field names:
var sColumns = "telephone1";
// Where clause filter
var sFilter = "contactid eq " + members[0].id;
var sOrderBy = "";
//Call the retrieve function
var requestResults = retrieveWebAPIData(sEntityName, sColumns, sFilter, sOrderBy);
//Loop through the results (there should only be one in this case):
if (requestResults != null) {
if (requestResults.value != null && requestResults.value.length > 0) {
for (var indRecordLoop = 0; indRecordLoop < requestResults.value.length; indRecordLoop++) {
var oContact = requestResults.value[indRecordLoop];
//Check returned data field for a value:
if(oContact.telephone1)
{
Xrm.Page.getAttribute("phonenumber").setValue(oContact.telephone1);
}
}
}
}
}
}
function retrieveWebAPIData(sEntityName, sSelect, sFilter, sOrderBy) {
var serverUrl = Xrm.Page.context.getClientUrl();
var ODATA_ENDPOINT = "/api/data/v9.0";
var odataUri = serverUrl + ODATA_ENDPOINT + "/" + sEntityName + "?";
if (sSelect) {
odataUri += "$select=" + sSelect;
}
if (sFilter) {
odataUri += "&$filter=" + sFilter;
}
if (sOrderBy){
odataUri += "&$orderby=" + sOrderBy;
}
var data;
try
{
var req = new XMLHttpRequest();
//alert(odataUri);
req.open("GET", encodeURI(odataUri), false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.send(null);
data = JSON.parse(req.responseText, dateReviver);
return data;
}
catch (ex){
alert("Error: retrieveWebAPIData - Data Set =" + odataSetName + "; filter = " + sFilter + "; select = " + sSelect + "; Error = " + ex.message);
}
}