Monday, February 21, 2011

calling crms service from java script in CRM2011

//calling crms service from java script in CRM2011

Here I am going to call CRM service using java script to retrieve data from a custom entity and assign the result values to the current form fields.

function SetBrand(brachid)
{

try
{


if(brachid!= null && brachid!= "" )
{
brachid= brachid.replace("%7b","").replace("%7d","");

var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();

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>" +
 // Specify the RetrieveMultiple message.
 "<RetrieveMultiple xmlns="+
 "\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
 // Specify that this is a QueryByAttribute query.
 "<query xmlns:q1="+
 "\"http://schemas.microsoft.com/crm/2006/Query\" "+
 "xsi:type=\"q1:QueryByAttribute\">" +
 // Query the customeraddress entity.
 "<q1:EntityName>new_branch</q1:EntityName>" +
 // Set the columns you want to return.
 "<q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
 "<q1:Attributes>" +
 "<q1:Attribute>new_brand</q1:Attribute>" +
 "</q1:Attributes>" +
 "</q1:ColumnSet>" +
 // Specify the attribute that you are querying on.
 "<q1:Attributes>" +
 "<q1:Attribute>new_branchid</q1:Attribute>" +
 "</q1:Attributes>" +
 // Set the value of the attribute using the customerid
 // value of the case record.
 "<q1:Values>" +
 "<q1:Value xsi:type=\"xsd:string\">"+
 brachid+
 "</q1:Value>" +
 "</q1:Values>" +
 "</query>" +
 "</RetrieveMultiple>" +
 "</soap:Body>" +
 "</soap:Envelope>";
// Create an instance of an XMLHTTP object.
 var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
// Configure the XMLHttp object for the
// Microsoft CRM Web services.
 xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
 xmlHttpRequest.setRequestHeader( "SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple" );
 xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8" );
 xmlHttpRequest.setRequestHeader(  "Content-Length", xml.length );
// Send the XMLHttp request.
 xmlHttpRequest.send(xml);
// Capture the XMLHttp response in XML format.
 var resultXml = xmlHttpRequest.responseXML;

// Create an XML document that you can parse.
 var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
 oXmlDoc.async = false;
// Load the document that has the results.
 oXmlDoc.loadXML(resultXml.xml);
 // Get only the BusinessEntity results.
 var businessEntities = oXmlDoc.getElementsByTagName('BusinessEntity');

// Loop through the BusinessEntity elements.
 for (i=0;i < businessEntities.length;i++)
 {
     if (businessEntities[i].selectSingleNode('./q1:new_brand') != null)
       {
    
        var NameElement = businessEntities[i].selectSingleNode('./q1:new_brand');
        // We already know it is not null.
        var NameValue = NameElement.text;
    // Set the value of the lookup field to the value of the array.

            Xrm.Page.getAttribute("new_brand").setValue(NameValue);
       }
  }
}
}
catch (e)
{}

}

No comments:

Post a Comment