Customizing CRM made easy


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:


  1. Open the Order entity from customization menu and open the ‘Form’.

  2. Open the properties of the Customer field and access the ‘onchange’ event. Add the following code, which makes a ‘query expression’ call to the crm server and retrieves the ‘pricelist’ associated with the customer. It subsequently assigns the price list of the sales order with this value. *Please remember to change the ‘MyServer’ to the crm server in the following code.

  3. Enable the event by clicking the check box next to ‘Event is Enabled’

        


   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

Comments (8)

  1. Gary Sherman says:

    Saw this code sample on the Microsoft Dynamics CRM blog, listed under Customizing CRM made easy: var

  2. Mihail Kotov says:

    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!

  3. dfoukas says:

    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

  4. Benjsoft says:

    Is there a better way to do it neatly without using SOAP? Like for HTTP Post/Get maybe?

  5. My apologies in not getting this up sooner. I flew home on the red-eye on Wednesday night, drove from

  6. dblake@coursemax.com says:

    Raj,

    This is an oldie but a goodie.  I have a small improvement:

    Replace:

    //    var serverUrl = "http://MyServer/mscrmservices/2006&quot;;

    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

  7. dblake@coursemax.com says:

    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.

    -Dan

  8. Anthony says:

    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.

    var ServerURL=SERVER_URL + "mscrmservices/2007";

    Thank

Skip to main content