Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

Count number of Activities per Opportunity

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!

Categories:
  • Suggested answer
    CRMJetty Profile Picture
    CRMJetty 3,508 on at
    RE: Count number of Activities per Opportunity

    Hello ChrisCadden,

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

      
        
        
        
        
        
        
        
        
        
        
      
    

  • Suggested answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Moderator on at
    RE: Count number of Activities per Opportunity

    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/.../

  • ChrisCadden Profile Picture
    ChrisCadden 5 on at
    RE: Count number of Activities per Opportunity

    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.

  • ChrisCadden Profile Picture
    ChrisCadden 5 on at
    RE: Count number of Activities per Opportunity

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

  • Suggested answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Moderator on at
    RE: Count number of Activities per Opportunity

    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/.../

  • Kipetcoff Profile Picture
    Kipetcoff 1,000 on at
    RE: Count number of Activities per Opportunity

    Hi,

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

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,537 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,520 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans