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 < and > 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 = "<?xml version='1.0' encoding='utf-8'?>"+
"<soap: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'>"+
authenticationHeader+
"<soap:Body>"+
"<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+
"<entityName>contact</entityName>"+
"<id>"+contactid+"</id>"+
"<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+
"<q1:Attributes>"+
"<q1:Attribute>fullname</q1:Attribute>"+
"<q1:Attribute>telephone1</q1:Attribute>"+
"<q1:Attribute>parentcustomerid</q1:Attribute>"+
"</q1:Attributes>"+
"</columnSet>"+
"</Retrieve>"+
"</soap:Body>"+
"</soap:Envelope>";
//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.
Subscribe to:
Post Comments (Atom)
1 comment:
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?
Post a Comment