Microsoft Dynamics 365 Blog

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

We're always looking for feedback and would like to hear from you. Please head to the Dynamics 365 Community to start a discussion, ask questions, and tell us what you think!