Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

Getting count of open opportunities under an Account

(0) ShareShare
ReportReport
Posted on by 140

Hi,

I wanted to get count of open opportunities associated to an account.I am using below code and when executed the query in browser I don't see any error or any data returning. What is wrong with the code?

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/OpportunitySet?$select=Name&$filter=AccountId/Id eq (guid'"+acctId+"') and StateCode/Value eq 0", true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
this.onreadystatechange = null;
if (this.status === 200) {
var returned = JSON.parse(this.responseText).d;
var results = returned.results;
for (var i = 0; i < results.length; i++) {
var name = results[i].Name;
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();

Thanks in Advance

  • Suggested answer
    gdas Profile Picture
    50,089 Moderator on at
    RE: Getting count of open opportunities under an Account

    Hi ,

    Why not you are using CRMRestBuilder to build your query easily .

    www.youtube.com/watch

  • Verified answer
    Ben Thompson Profile Picture
    6,350 on at
    RE: Getting count of open opportunities under an Account

    Actually the code is wrong as it's reference the 2011 Soap end point rather than the v8 and above web api end points.

    try

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/Opportunities?$select=Name&$filter=AccountId/Id eq (guid'"+acctId+"') and StateCode/Value eq 0", true);

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

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

    req.onreadystatechange = function () {

    if (this.readyState === 4) {

    this.onreadystatechange = null;

    if (this.status === 200) {

    var returned = JSON.parse(this.responseText).d;

    var results = returned.results;

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

    var name = results[i].Name;

    }

    } else {

    Xrm.Utility.alertDialog(this.statusText);

    }

    }

    };

    req.send();

  • Suggested answer
    Mahadeo Matre Profile Picture
    17,021 on at
    RE: Getting count of open opportunities under an Account

    Hi..

    Your code is correct.. you will get count of open opportunities from results.length

    Also you can try using calculated / rollup field to get count of open opportunities.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,863 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,723 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans