+ Start a Discussion
AnuShAnuSh 

How do we count Unique services based on opportunity

Hi Guys,
I'm new to developing

,User-added image


Can anybody help me in writing triggers
I have three objects 
opportunity: object
Service_Offered__C :object 
Product: Object
Same service and same vendor Considered as one.
Regarding above screen shot scenario: Four unique services 
Requirement: I would like to count unique services offered based on opportunity.


Let me know if you have any questions. Thanks in advance .
 
 
Vivian Charlie 1208Vivian Charlie 1208

Hi Anush,

 

You can use aggregate queries to find distinct count of records. The following example searches for distinct contacts where the Custom_Industry__c field has a unique value and groups it by the Account Id.

List<AggregateResult> lstC = [Select AccountId, COUNT_DISTINCT(Custom_Industry__c) disc from Contact Group By AccountId] ;
for (AggregateResult ar : lstC) {
     System.debug('Account ID*****' + ar.get('AccountId')); // Account Id
     System.debug('Count*****' + ar.get('disc')); // Count of Contacts where Custom_Industry__c is distinct
}

COUNT_DISTINCT (desired fields to check unique) // for your use case you can create a formula field ServiceName+Vendor and use this in the COUNT_DISTINCT method. You can add desired filters to the where clause.

 

Thanks

Vivian