I’m working with CRM Dynamics. There is a JavaScript that uses ActiveXObject to resolve a request but I need to convert that to a Fetch Request.
The old code is this:
function fnSetStateActiveQuoteRequest() {
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\">" +
GenerateAuthenticationHeader() +
" <soap:Body>" +
" <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <Request xsi:type=\"SetStateQuoteRequest\">" +
" <EntityId>" + Xrm.Page.data.entity.getId() + "</EntityId>" +
" <QuoteState>Active</QuoteState>" +
" <QuoteStatus>2</QuoteStatus>" +
" </Request>" +
" </Execute>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
and I’m trying to convert that with:
var _oService;
var _sOrgName = "myOrg";
var _sServerUrl = Xrm.Page.context.getServerUrl();
function fnSetStateActiveQuoteRequest(){
var sFetch = '<fetch mapping="logical">';
sFetch+= '<entity name='+Xrm.Page.data.entity.getId()'>';
sFetch+= '</filter>';
sFetch+= '</entity>';
sFetch+= '</fetch>';
_oService = new FetchUtil(_sOrgName, _sServerUrl);
var oEntity = _oService.Fetch(sFetch, myCallBack);
}
But I have no idea how to declare the Request> and the QuoteState>
The problem you’re having is the first XML you are looking at is the serialized version of an execute request. Fetch is more of a Query Language so you write it slightly differently.
The easiest way to figure out what the fetch xml needs to look like is open up an advanced find and perform the search manually. The Advanced Find gives you a “Download Fetch XML” button to save you the hassle of writing all the fetch!
So, the old query looked like it was trying to return all records of entity “X” where it was Active and had a status reason of 2. If I write something similar in advanced find (let’s say I’m looking for opportunities) my query might look like this:
That was pretty much a copy and paste from the Download Fetch XML of an advanced find.