web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

i am trying retrieve all activities which are associated with the opportunity record by using webApi,my code goes as follows,the error it shpws is Failed to load resource: the server responded with a status of 500 (Internal Server Error),and invalid xml er

(0) ShareShare
ReportReport
Posted on by

function activity_close() {
debugger;
var oppId = Xrm.Page.data.entity.getId().substring(1, 37);
var activityFetchXML = ['<fetch distinct="false" mapping="logical" output-format="xml - platform" version="1.0">'+
'< entity name = "activitypointer" >'+
'< attribute name = "statecode" />'+
'< attribute name = "activityid" />'+
'< filter type = "and" >'+
'< condition attribute = "regardingobjectid" operator= "eq" value = ' + oppId + ' />'+
'</ filter >' +
'</ entity >' +
'</ fetch >'].join('');
var encodedFetchXML = encodeURIComponent(activityFetchXML);
var data = {};
var serverURL = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest();
req.open("GET", serverURL + "/api/data/v9.0/activitypointers?fetchXml=" + encodedFetchXML, true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
req.onreadystatechange = function () {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 200) {
data = JSON.parse(this.responseText);
var results = new Array();
results = data.value;
for (var i = 0; i <= results.length; i++) {
var statecode = results[i]["statecode"];
var primaryActivityId = results[i]["activityid"];
if (statecode != 1) {
alert("opportunity cannot be closed since activities are not completed or canceled");
}

}

} else {
var error = JSON.parse(this.responseText).error;
(error.message);
}
}
};
req.send();
}

can anybody please suggest what could be wrong in the above code,i am trying retrieve all activities which are associated with the opportunity record by using webApi fetchXml querying method.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi Sandesh,

    1.First of all i would suggest you to test your fetch XML :

    http://msxrmtools.com/fetchxml

    2. another thing you can do is to install rest  builder in your dev and try the same code and execute it there.

  • Verified answer
    Community Member Profile Picture
    on at

    got solution,it was just invalid xml correct format goes like this

    function activity_close() {

       debugger;

       var oppId = Xrm.Page.data.entity.getId().substring(1, 37);

    var flag=0;

       var activityFetchXML ="<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>"+

           "<entity name='activitypointer'>" +

           "<attribute name = 'statecode'/>" +

           "<attribute name = 'activityid'/>" +

           "<filter type = 'and'>"+"<condition attribute = 'regardingobjectid' operator= 'eq' value = '" + oppId + "'/>"+

           "</filter>"+

           "</entity>"+

           "</fetch>";

       var encodedFetchXML = encodeURIComponent(activityFetchXML);

       var data = {};

       var serverURL = Xrm.Page.context.getClientUrl();

       var req = new XMLHttpRequest();

       req.open("GET", serverURL + "/api/data/v9.0/activitypointers?fetchXml=" + encodedFetchXML, true);

       req.setRequestHeader("Accept", "application/json");

       req.setRequestHeader("OData-MaxVersion", "4.0");

       req.setRequestHeader("OData-Version", "4.0");

       req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

       req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");

       req.onreadystatechange = function () {

           if (this.readyState == 4) {

               req.onreadystatechange = null;

               if (this.status == 200) {

                   data = JSON.parse(this.responseText);

                   var results = new Array();

                   results = data.value;

                   for (var i = 0; i <results.length; i++)

                   {

                       var statecode = results[i]["statecode"];

                       var primaryActivityId = results[i]["activityid"];

                       if (statecode != 1) {

                           alert("opportunity cannot be closed since activities are not completed or canceled");

    flag=1;

                           break;

                       }

                   }

    if(flag==0){

    alert("HELLO SANDESH,opportunity can be closed since activities are  canceled or completed");

    }

               } else {

                   var error = JSON.parse(this.responseText).error;

                   (error.message);

               }

           }

       };

       req.send();

    }

    just replace ' with " and vice versa

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans