Get Max value using Javascript

 If you want to fetch Max value from CRM entity record using Javascript, you can use below code, remember to change entity and field name accordingly

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>”+

“<RetrieveMultiple xmlns=’http://schemas.microsoft.com/crm/2007/WebServices’>”+

“<query xmlns:q1=’http://schemas.microsoft.com/crm/2006/Query'”+

” xsi:type=’q1:QueryExpression’>”+

“<q1:EntityName>EntityName</q1:EntityName>”+ //change entity name

“<q1:ColumnSet xsi:type=’q1:ColumnSet’>”+

“<q1:Attributes>”+

“<q1:Attribute>FieldName</q1:Attribute>”+ //change field name

“</q1:Attributes>”+

“</q1:ColumnSet>”+

“<q1:Orders>” +

“<q1:Order>” +

“<q1:AttributeName>FieldName</q1:AttributeName>” + //change field name

“<q1:OrderType>Descending</q1:OrderType>” +

“</q1:Order>” +

“</q1:Orders>” +

“</query>”+

“</RetrieveMultiple>”+

“</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/RetrieveMultiple”);

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);

}

else

{

var results = resultXml.getElementsByTagName(‘BusinessEntity’);

var msg = “”;

if (results.length == 0)

{

alert(msg);

return;

}

else

{

if(results[0].selectSingleNode(‘./q1:FieldName’)!=null)

{

var MaxValue = results[0].selectSingleNode(‘./q1:FieldName’).nodeTypedValue;   //change name of the field to get Max value

alert(MaxValue);

}

}}

Leave a Reply

Your email address will not be published. Required fields are marked *