Experience your
30 day trial
now!
GET STARTED
The customization tools of Microsoft CRM are very flexible and it is very easy to extend the base functionality with modest changes. I chose the following example to demonstrate this.
In Microsoft CRM, while creating a new order or quote, user is required to specify the pricelist in addition to the customer.
However, in most scenarios, customer already has a previous pricing agreement and it would be nice if the pricelist is defaulted from the customer. By calling the CRM web service from client we can retrieve the pricelist of the customer. The following example provides detailed steps to accomplish this:
4. Publish the changes.
//code
//Get the customerid
var lookupItem = new Array;
lookupItem = crmForm.all.customerid.DataValue;
// Proceed only if customer Id is not null
if (lookupItem[0] != null && lookupItem[0].id != null)
{
var id = lookupItem[0].id;
//TODO Replace 'MyServer' with crm server
var serverUrl = "http://MyServer/mscrmservices/2006";
//Create a http request
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("POST", serverUrl + "/crmservice.asmx", false);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8") ;
xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2006/WebServices/RetrieveMultiple") ;
//Send query to retrieve pricelist of the customer
xmlhttp.send("<?xml version='1.0' encoding='utf-8'?>"+"\n\n"+"<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">'+
' <soap:Body>' +
' <query xmlns:q1=http://schemas.microsoft.com/crm/2006/Query’ +
‘ xsi:type="q1:QueryExpression" xmlns="http://schemas.microsoft.com/crm/2006/WebServices">'+
' <q1:EntityName>pricelevel</q1:EntityName>'+
' <q1:ColumnSet xsi:type="q1:ColumnSet">'+
' <q1:Attributes>'+
' <q1:Attribute>pricelevelid</q1:Attribute>'+
' <q1:Attribute>name</q1:Attribute>'+
' </q1:Attributes>'+
' </q1:ColumnSet>'+
' <q1:Distinct>false</q1:Distinct>'+
' <q1:LinkEntities>'+
' <q1:LinkEntity>'+
' <q1:LinkFromAttributeName>pricelevelid</q1:LinkFromAttributeName>'+
' <q1:LinkFromEntityName>pricelevel</q1:LinkFromEntityName>'+
' <q1:LinkToEntityName>account</q1:LinkToEntityName>'+
' <q1:LinkToAttributeName>defaultpricelevelid</q1:LinkToAttributeName>'+
' <q1:JoinOperator>Inner</q1:JoinOperator>'+
' <q1:LinkCriteria>'+
' <q1:FilterOperator>And</q1:FilterOperator>'+
' <q1:Conditions>'+
' <q1:Condition>'+
' <q1:AttributeName>accountid</q1:AttributeName>'+
' <q1:Operator>Equal</q1:Operator>'+
' <q1:Values>'+
' <q1:Value xmlns:q2="http://microsoft.com/wsdl/types/"'+
' xsi:type="q2:guid">' +
id +
' </q1:Value>'+
' </q1:Values>'+
'</q1:Condition>'+
' </q1:Conditions>'+
'</q1:LinkCriteria>'+
' </q1:LinkEntity>'+
' </q1:LinkEntities>'+
' </query>'+
' </soap:Body>'+
' </soap:Envelope>') ;
var result = xmlhttp.responseXML.xml;
//Parse the result to obtain the pricelist details
var doc = new ActiveXObject("Microsoft.XMLDOM");
doc.loadXML(result);
var priceId = doc.selectSingleNode("//BusinessEntities[0]/BusinessEntity/pricelevelid");
var priceName = doc.selectSingleNode("//BusinessEntities[0]/BusinessEntity/name");
if(priceId != null && priceName != null)
//Create a new lookup object
var lookItm = new Array();
lookItm[0] = new LookupControlItem (priceId.text , 1022, priceName.text);
// Set the form control value to the lookupItem just created.
crmForm.all.pricelevelid.DataValue = lookItm ;
}
Enjoy,
Raj Bakam
Saw this code sample on the Microsoft Dynamics CRM blog, listed under Customizing CRM made easy: var
I am trying to get first & last name of the contact with the given ID. My only problem is that I can't build correct query for xmlhttp.send
Can you post here or send me a correct query, that I should use for getting any filed of the contact, which ID I retrieve from the standard lookup.
This is really vital for me! Thanks!
Hi Raj,
It seems that either the code or the cut & paste is broken! Can you email me your sample 'cause it's very interesting to us...
dimitris@mantis.gr
Is there a better way to do it neatly without using SOAP? Like for HTTP Post/Get maybe?
My apologies in not getting this up sooner. I flew home on the red-eye on Wednesday night, drove from
Raj,
This is an oldie but a goodie. I have a small improvement:
Replace:
// var serverUrl = "http://MyServer/mscrmservices/2006";
with the following line:
var ServerURL=SERVER_URL + "mscrmservices/2007";
If you use the SERVER_URL variable, this code will work unaltered with any deployment (even IFD).
Also, anyone using this code, be sure to change any ` characters to ' or you'll get errors.
-Dan
Of course the price list is defaulted automatically in CRM 4.0 but this is a good example of using CRM web services through Java Script.
Just to verify whether the following coding valid for CRM OUTLOOK client? I tried 4 years back. But it hit error when using crm outlook client.
Thank