Monday, January 31, 2011

CRM 4.0: Phone Call Entity - Populate Phone Number

Use the code below to populate the phone number field on a phone call record from a lead or contact (regarding object). Also, you can populate the memo/description field to contain all available numbers (home, business, and/or mobile).



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Populate Phone Number Field - by LBMC, Josh Yockey//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(crmForm.FormType == 1 && crmForm.all.regardingobjectid.DataValue != null
&& (crmForm.all.regardingobjectid.DataValue[0].typename == "lead" || crmForm.all.regardingobjectid.DataValue[0].typename == "contact"))
{
var xml = "" +
"" +
"" +
GenerateAuthenticationHeader() +" " +
" " +
" crmForm.all.regardingobjectid.DataValue[0].typename" +
" "+ crmForm.all.regardingobjectid.DataValue[0].id + "" +
" " +
" " +
" telephone2" +
" mobilephone" +
" telephone1" +
"
" +
"
" +
"
" +
"
" +
"
" +
"";

var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);

var resultXml = xmlHttpRequest.responseXML;

var home = "";
var mobile = "";
var business = "";
var desc = "";
crmForm.all.phonenumber.DataValue = "";

var node = resultXml.selectSingleNode("//q1:telephone2");
if (node != null)
home = resultXml.selectSingleNode("//q1:telephone2").text;

node = resultXml.selectSingleNode("//q1:mobilephone");
if (node != null)
mobile = resultXml.selectSingleNode("//q1:mobilephone").text;

node = resultXml.selectSingleNode("//q1:telephone1");
if (node != null)
business = resultXml.selectSingleNode("//q1:telephone1").text;

if (home.length > 0)
{
crmForm.all.phonenumber.DataValue = home;
desc += "\nHome: " + home;
}
if (mobile.length > 0)
{
if (crmForm.all.phonenumber.DataValue == null || crmForm.all.phonenumber.DataValue.length == 0)
{
crmForm.all.phonenumber.DataValue = mobile;
}
desc += "\nMobile: " + mobile;
}
if (business.length > 0)
{
if (crmForm.all.phonenumber.DataValue == null || crmForm.all.phonenumber.DataValue.length == 0)
{
crmForm.all.phonenumber.DataValue = business;
}
desc += "\nBusiness: " + business;
}

crmForm.all.description.DataValue = desc;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

No comments:

Post a Comment