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
Rishant KumarRishant Kumar 

Hi guys, I am write a trigger from opportunity line itme to find the total number of unique product count please help

trigger findingoppcount on OpportunityLineItem (after insert) {
    List<Account> acc = new List<Account>();
    Set<Id> oppId = new Set<Id>();
    Map<Id,List<OpportunityLineItem>> Account_opplineitems = new Map<Id,List<OpportunityLineItem>> ();
    
    for( OpportunityLineItem oppLine : Trigger.New ){
        oppId.add( oppLine.OpportunityId );
        system.debug('oppid'+oppId);
    }
    
    
    
    for( Opportunity opp : [ SELECT Id, AccountId,
                            ( SELECT Id, Product2Id FROM OpportunityLineItems ) 
                            FROM Opportunity WHERE Id IN: oppId ] ){
                                for(OpportunityLineItem oli_obj :opp.OpportunityLineItems){
                                    if(Account_opplineitems.containsKey(opp.AccountId) && Account_opplineitems.get(opp.AccountId) != null) {
                                        List<OpportunityLineItem> lst_terr = Account_opplineitems.get(opp.AccountId);
                                        lst_terr.add(oli_obj);
                                        Account_opplineitems.put(opp.AccountId,lst_terr); 
                                    }   
                                    else {
                                        Account_opplineitems.put(opp.AccountId, new List<OpportunityLineItem> {oli_obj});
                                    }
                                    
                                }                                
                            }
    system.debug('Account_opplineitems'+Account_opplineitems);
    
}
Suraj Tripathi 47Suraj Tripathi 47
Hi Rishant Kumar,

trigger findingoppcount on OpportunityLineItem (after insert) {
    try{
        List<Account> acc = new List<Account>();
        Set<Id> oppId = new Set<Id>();
        Map<Id,List<OpportunityLineItem>> Account_opplineitems = new Map<Id,List<OpportunityLineItem>> ();
        
        for( OpportunityLineItem oppLine : Trigger.New ){
            oppId.add( oppLine.OpportunityId );
            system.debug('oppid'+oppId);
        }
        
        for( Opportunity opp : [ SELECT Id, AccountId,
                                ( SELECT Id, Product2Id FROM OpportunityLineItems ) 
                                FROM Opportunity WHERE Id IN: oppId And AccountId !=Null ] ){
                                    //Add a null check for AccoundID not be Null
                                    //And check opportunity has some OpportunityLineItems
                                    if(opp.OpportunityLineItems.size()>0){
                                        for(OpportunityLineItem oli_obj :opp.OpportunityLineItems){
                                            if(Account_opplineitems.containsKey(opp.AccountId) && Account_opplineitems.get(opp.AccountId) != null) {
                                                List<OpportunityLineItem> lst_terr = Account_opplineitems.get(opp.AccountId);
                                                //flag to identify unique products by product2Id
                                                boolean isUnique = true;
                                                for(OpportunityLineItem oli :lst_terr){
                                                    if(oli.Product2Id == oli_obj.Product2Id){
                                                        isUnique = false;
                                                    }
                                                }
                                                if(isUnique == true){
                                                    lst_terr.add(oli_obj);
                                                    Account_opplineitems.put(opp.AccountId,lst_terr); 
                                                }
                                            }   
                                            else {
                                                Account_opplineitems.put(opp.AccountId, new List<OpportunityLineItem>());
                                            }
                                        }   
                                    }
                                    
                                }
        
        for( Id varId : Account_opplineitems.keySet()){
            system.debug('AccountId----->'+varId+' And Number of unique product----------->'+Account_opplineitems.get(varId).size());
        }
    }
    
    catch(Exception ex){
        system.debug('*****Error*****'+ex.getMessage()+'****Line Number ******'+ex.getLineNumber());
    }
    
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Suraj Tripathi
Rishant KumarRishant Kumar

sir i want to store a value it in a field 
i make a field in account obj name count__c 
 

Rishant KumarRishant Kumar
Sir I want to store a value in field i make a field in account obj that name count __c please send me a solution
Suraj Tripathi 47Suraj Tripathi 47
Hi Rishant Kumar,

Here is updated code:

trigger findingoppcount on OpportunityLineItem (after insert) {
    try{
        List<Account> acc = new List<Account>();
        Set<Id> oppId = new Set<Id>();
        Map<Id,List<OpportunityLineItem>> Account_opplineitems = new Map<Id,List<OpportunityLineItem>> ();
        
        for( OpportunityLineItem oppLine : Trigger.New ){
            oppId.add( oppLine.OpportunityId );
            system.debug('oppid'+oppId);
        }
        
        for( Opportunity opp : [ SELECT Id, AccountId,
                                ( SELECT Id, Product2Id FROM OpportunityLineItems ) 
                                FROM Opportunity WHERE Id IN: oppId And AccountId !=Null ] ){
                                    //Add a null check for AccoundID not be Null
                                    //And check opportunity has some OpportunityLineItems
                                    if(opp.OpportunityLineItems.size()>0){
                                        for(OpportunityLineItem oli_obj :opp.OpportunityLineItems){
                                            if(Account_opplineitems.containsKey(opp.AccountId) && Account_opplineitems.get(opp.AccountId) != null) {
                                                List<OpportunityLineItem> lst_terr = Account_opplineitems.get(opp.AccountId);
                                                //flag to identify unique products by product2Id
                                                boolean isUnique = true;
                                                for(OpportunityLineItem oli :lst_terr){
                                                    if(oli.Product2Id == oli_obj.Product2Id){
                                                        isUnique = false;
                                                    }
                                                }
                                                if(isUnique == true){
                                                    lst_terr.add(oli_obj);
                                                    Account_opplineitems.put(opp.AccountId,lst_terr); 
                                                }
                                            }   
                                            else {
                                                Account_opplineitems.put(opp.AccountId, new List<OpportunityLineItem>());
                                            }
                                        }   
                                    }
                                    
                                }
        
        List<Account> updateAccountList = [SELECT Id, count__c FROM Account WHERE Id IN :Account_opplineitems.keySet()];
        
        for( Account accObj : updateAccountList){
            if(Account_opplineitems.containsKey(accObj.id)){
                accObj.count__c = Account_opplineitems.get(accObj.id).size();
            }
        }

        system.debug('updateAccountList----->'+updateAccountList);
        
        // update count field on Account object
        update updateAccountList;
    }
    catch(Exception ex){
        system.debug('*****Error*****'+ex.getMessage()+'****Line Number ******'+ex.getLineNumber());
    }
}

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Suraj Tripathi
Lizitha sri KodaliLizitha sri Kodali
Hi Suraj, I want to get all opportunity line items on account custom field . When we are updating any opplineitems it should be updated on account also. Can you please provide me code