-
Run Fetch XML Query
JavaScript
Executes FetchXML query to retrieve opportunities linked to a specific account
This Dynamics 365 script executes a FetchXML query to retrieve opportunities linked to a specific account and logs the results.
function RunFetchXMLQuery(executionContext) {
const formContext = executionContext.getFormContext();
let accountId = formContext.data.entity.getId();
let xml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'><entity name='opportunity'><attribute name='opportunityid' /><attribute name='name' /><attribute name='statuscode' /><attribute name='createdon' /><filter type='and'><condition attribute='parentaccountid' operator='eq' value='" + accountId + "' /></filter></entity></fetch>";
let fetchxml = "?fetchXml=" + encodeURIComponent(xml);
Xrm.WebApi.retrieveMultipleRecords("opportunity", fetchxml).then(function success(result) {
var opportunities = result.entities;
console.log(opportunities);
},
function (error) {
console.warn(error);
});
}