window.ExecFetchXML = function (entityName, column, conditionAttribute, operator, value, fieldOrderName, fieldOrderValue, page, count) {
var fetchMapping = "logical";
var filterType = "and";
var authenticationHeader = GenerateAuthenticationHeader();
var columnSplit = column.split(",");
var conditionAttributeSplit = conditionAttribute.split(",");
var operatorSplit = operator.split(",");
var valueSplit = value.split(",");
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>" +
"<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<fetchXml><fetch mapping='" + fetchMapping + "'";
if (page != null) xml += " page='" + page + "' ";
if (count != null) xml += " count='" + count + "' ";
xml += "><entity name='" + entityName + "'>";
if (columnSplit.length > 1) {
for (i = 0; i < columnSplit.length; i++) {
xml += "<attribute name='" + String(columnSplit[i]) + "'/>";
}
}
else {
xml += "<attribute name='" + column + "'/>";
}
if (fieldOrderName != null && fieldOrderValue != null) {
xml += "<order attribute='" + fieldOrderName +
"' descending='" + fieldOrderValue + "'/>";
}
if (conditionAttribute != null && operator != null && value != null) {
xml += "<filter type='" + filterType + "'>";
if (conditionAttributeSplit.length > 1) {
for (i = 0; i < conditionAttributeSplit.length; i++) {
xml += "<condition attribute='" + conditionAttributeSplit[i] + "'" +
" operator='" + operatorSplit[i] + "' value='" + valueSplit[i] + "'/>";
}
}
else {
xml += "<condition attribute='" + conditionAttribute + "'" +
" operator='" + operator + "' value='" + value + "'/>";
}
xml += "</filter>";
}
xml += "</entity>" +
"</fetch></fetchXml>" +
"</Fetch>" +
"</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/Fetch");
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) {
// Capture the result and UnEncode it.
var resultSet = new String();
resultSet = resultXml.text;
resultSet = resultSet.replace("<resultset morerecords=\"0\" />", "");
if (resultSet != "") {
// Create an XML document that you can parse.
var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false;
// Load the XML document that has the UnEncoded results.
oXmlDoc.loadXML(resultSet);
var retValue = null;
if (oXmlDoc.getElementsByTagName('result') != null) {
var objLen = oXmlDoc.getElementsByTagName('result').length;
retValue = new Array(objLen);
// Loop nas Linhas
for (j = 0; j < objLen; j++) {
retValue[j] = new Object();
if (columnSplit.length > 1) {
for (i = 0; i < columnSplit.length; i++) {
if (oXmlDoc.getElementsByTagName('result')[j].selectSingleNode(String(columnSplit[i])))
eval("retValue[" + String(j) + "]." + String(columnSplit[i]) + "= '" + oXmlDoc.getElementsByTagName('result')[j].selectSingleNode('./' + String(columnSplit[i])).text + "'");
}
}
else {
if (oXmlDoc.getElementsByTagName('result')[j].selectSingleNode(column))
eval("retValue[" + String(j) + "]." + column + "= '" + oXmlDoc.getElementsByTagName('result')[j].selectSingleNode(column).text + "'");
}
}
}
}
}
return retValue;
}
E as formas de utilização:
var result1 = window.ExecFetchXML('account', 'accountid, name', 'name', 'like', '%TESTE%', null, null, null, null);
alert(result1[0].accountid);
alert(result1[0].name);
/*
- Ordenado por [name];
- Ascendente [true];
- Retorna 2 páginas, com 10 clientes por página.
*/
var result2 = window.ExecFetchXML('account', 'accountid, name', 'name', 'eq', 'TESTE', 'name', 'true', 2, 10);
/*
- Busca por vários campos (name like '%TESTE%' e cnpj='12345678900');
*/
var result3 = window.ExecFetchXML('account', 'accountid, name', 'name,cnpj', 'like,eq', '%TESTE%,12345678900', null, null, null, null);
Nenhum comentário:
Postar um comentário
<< Ao enviar um comentário, favor clicar na opção [Enviar por e-mail comentários de acompanhamento para gtezini@gmail.com] >>