function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

OPPORTUNITY Won/Lost Statistics for User

Hey guys,

I want to track how many opportunities each sales user has lost/won. My end result looks like this:


Name   |    Won   |    Lost

ABC             23           67

XYZ              12           35



My own idea is to create two custom number fields Opps_Won__c, Opps_Lost__c for the object user.

Everytime an opportunity's stage is changed to Won/Lost a trigger fires and increases the number in the corresponding custom field of the opportunity's owner.


I could also get the won/lost counts with a SOQL query for each sales user, but then I would get in conflict with the query limit, because I want to have an overview over all sales users and I have a lot of sales users....


Any other ideas how I could realize this?

Best Answer chosen by Admin (Salesforce Developers) 

Ok, pretty simple.

I can get the needed data with 1 SOQL query:


SELECT Owner.Name, count(id), StageName
FROM Opportunity
WHERE IsWon = true
OR (IsWon = false AND IsClosed = true)
GROUP BY Owner.Name, StageName