Skip to main content
Anush G fragte in #Apex
Hi Guys,

I'm new to developing

,How do we count Unique services based on opportunity

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 .

 

 

1 Antwort
  1. 11. Apr. 2017, 11:41

    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

0/9000