Friday, July 18, 2008

HOWTO: MS CRM 4.0 Show Phone Number on Phone Call Form

I have recently started working with Microsoft CRM 4.0 and have been smitten a bit by its capabilities. However I do find that out of the box there are some simple things missing that would have been nice to provide.

One of the main items is this: if you open a new Phone Call form and select the Recipient, you need to manually enter the phone number into the Phone Number textbox.

After lots of hair pulling I have put together the solution. Much of the 'customization' done effectively for CRM can be accomplished by reading from CRM web services. Below is a sample set of code in Javascript that I put behind the OnChange event of the Recipient field on the Phone Call form, to get this to work.

(Note: it displays only the first telephone number on record - I will add more functionality later that will allow the selection from a list of numbers allocated against the contact
. Also you may need to replace the &lt and &gt with <> respectively as this is html encoded.).


var lookupItem = new Array;

// Get the lookup for the primarycontactid attribute on the account form.
lookupItem = crmForm.all.to.DataValue;


if (lookupItem && lookupItem[0]!=null) {

var contactGUID = lookupItem[0].id;

//Prepare variables for a contact to retrieve.
var contactid = contactGUID;
var authenticationHeader = GenerateAuthenticationHeader();


//Prepare the SOAP message.
var xml = "&lt?xml version='1.0' encoding='utf-8'?&gt"+
"&ltsoap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"+
" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'&gt"+
authenticationHeader+
"&ltsoap:Body&gt"+
"&ltRetrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'&gt"+
"&ltentityName&gtcontact&lt/entityName&gt"+
"&ltid&gt"+contactid+"&lt/id&gt"+
"&ltcolumnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'&gt"+
"&ltq1:Attributes&gt"+
"&ltq1:Attribute&gtfullname&lt/q1:Attribute&gt"+
"&ltq1:Attribute&gttelephone1&lt/q1:Attribute&gt"+
"&ltq1:Attribute&gtparentcustomerid&lt/q1:Attribute&gt"+
"&lt/q1:Attributes&gt"+
"&lt/columnSet&gt"+
"&lt/Retrieve&gt"+
"&lt/soap:Body&gt"+
"&lt/soap:Envelope&gt";

//Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);

xHReq.send(xml);

//Capture the result.
var resultXml = xHReq.responseXML;

//Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}
//Display the retrieved value.
else
{
crmForm.all.phonenumber.value = resultXml.selectSingleNode("//q1:telephone1").nodeTypedValue;
crmForm.all.subject.focus();
}

}


Seems to work like a charm, now I don't need to open multiple windows to do my job.

1 comment:

Unknown said...

Thanks for this! great code.

I am having a slight problem, when the phone number is missing on the contact, I get an error when the Phone Call form opens (Object Required)

Can you point me in the right direction?