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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Count number of Activities per Opportunity

(0) ShareShare
ReportReport
Posted on by 5

Hi Everyone,

Please assist, my requirement is to get a count of all activities per opportunity.

i.e

OpportunityId: e90a0493-e8f0-ea11-a815-000d3a1b14a2

Activities: 2 Emails, 3 Logged Calls

Output: 

OpportunityId: e90a0493-e8f0-ea11-a815-000d3a1b14a2

Activity Count: 5

How would I go about retrieving this via the API endpoints? I need to loop through all opportunities and attain this count for all opportunities .

Thanks!

I have the same question (0)
  • Kipetcoff Profile Picture
    1,037 on at

    Hi,

    Is it necessary to get the amount through the API? Where will this data be used then?

  • Suggested answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi ChrisCadden

    Here is the JS codee

    let globalVariables = {
        Results: ""
    };

    function function1(_opportunityid) {
    try {
    var fetchData = {
    "opportunityid": _opportunityid
    };
    var fetchXml = [
    "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>",
    " <entity name='activitypointer'>",
    " <attribute name='activitytypecode'/>",
    " <attribute name='activityid'/>",
    " <link-entity name='opportunity' from='opportunityid' to='regardingobjectid' link-type='inner' alias='aa'>",
    " <filter type='and'>",
    " <condition attribute='opportunityid' operator='eq' uiname='ANIA Centres Partnership 2015' uitype='opportunity' value='", fetchData.opportunityid/*{98E4A92C-752B-E511-80FC-C4346BADF6A4}*/, "'/>",
    " </filter>",
    " </link-entity>",
    " </entity>",
    "</fetch>"
    .join("");
    function2("maa_donationdetailses", fetchXml);

    if (globalVariables.Results.length > 0) {
    //here you are
    }
    } catch (e) {
    alert("function1: " e.message);
    }
    }

    function function2(entitysPluralName, fetchXmlQuery) {
    try {
    var req = new XMLHttpRequest();
    req.open(
    "GET",
    Xrm.Page.context.getClientUrl()
    "/api/data/" globalVariables.WebAPIVersion "/" entitysPluralName "?fetchXml="
    encodeURIComponent(fetchXmlQuery),
    false
    );//Sync
    req.setRequestHeader("Prefer", 'odata.include-annotations="*"');
    req.onreadystatechange = function () {
    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 200) {
    var results = JSON.parse(this.response);
    globalVariables.Results = results.value;
    } else {
    alert(this.statusText);
    }
    }
    };
    req.send();
    } catch (e) {
    alert("function2: " e.message);
    }
    }

    If I answer your question then please mark it as verified.

    Let me know if I can provide you with more details.

    Thanks
    Regards,

    Abdul Wahab
    Power Platform & Customer Engagement Developer/Lead/Solution Architecture/Project Manager
    Direct/WhatsApp: 923323281237
    E-mail: abdulwahabubit@outlook.com
    Skype: abdul.wahabubit
    Linkedin: www.linkedin.com/.../

  • ChrisCadden Profile Picture
    5 on at

    Yes it is necessary, we will be using this count/amount in an external system.

  • ChrisCadden Profile Picture
    5 on at

    Thanks for your reply Abdul Wahab

    Perhaps I need to supply some more context, we are using the opportunities API to fetch all the deals and storing them in our database.

    Using the following:

    /api/data/v9.0/opportunities?$expand=transactioncurrencyid($select=isocurrencycode),owninguser($select=firstname,lastname)&$filter=statecode%20eq%200

    Based on the above endpoint, how can I now get a count of all the activities for these deals? Using the API's only.

    Thanks,

    Chris.

  • Suggested answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi ChrisCadden,

    In case you used my described method then you need know about two things.

    1. Advanced Find: Make your query on it. Download the query.
    2. Open it in Fetch XML Builder on the XRM Toolbox. Format your query in JS

    It is very simple if compared with your one.

    If I answer your question then please mark it as verified.

    Let me know if I can provide you with more details.

    Thanks
    Regards,

    Abdul Wahab
    Power Platform & Customer Engagement Developer/Lead/Solution Architecture/Project Manager
    Direct/WhatsApp:+923323281237
    E-mail: abdulwahabubit@outlook.com
    Skype: abdul.wahabubit
    Linkedin: www.linkedin.com/.../

  • Suggested answer
    CRMJetty Profile Picture
    3,512 on at

    Hello ChrisCadden,

    Using below shown FetchXMl you can get all activites for particular opportunity:

      
        
        
        
        
        
        
        
        
        
        
      
    

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans