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
Manju053Manju053 

Help required to design an Helper Class for Both Trigger and Apex

Hello Developers 

I am new to the Apex Development, we have multiple triggers on a Opportunity, So i have been Given a Task to create a OpportunityHelper Class where all the Logic has to be Implemented in a Single Class, i am getting some errors where i am not able to resolve it

This is my below Trigger Logic
 
trigger opportunityTrigger on Opportunity (after delete,after update,after insert,after undelete) {
   
    set<ID>QtaIds = new set<ID>(); 
    if(trigger.isinsert || trigger.isundelete){
        for(opportunity opp : trigger.new){
            QtaIds.add(opp.Quota__c);
        }
    }  
    if(trigger.isdelete){
        for(opportunity opp : trigger.old){
            QtaIds.add(opp.Quota__c);           
        }        
    }
    if(trigger.isupdate){
        for(opportunity opp:trigger.new){
            QtaIds.add(opp.Quota__c);
            if(trigger.oldmap.get(opp.id).Quota__c != opp.Quota__c && trigger.oldmap.get(opp.id).Quota__c != null ){
                QtaIds.add(trigger.oldmap.get(opp.id).Quota__c);
            }            
        }
    }    
    map<id,double> amtmap = new map<id,double>();
    for(aggregateresult ag : [SELECT SUM(Amount) sum,Quota__c FROM Opportunity where Quota__c in :QtaIds and ClosedDate_Checkbox__c ='True' and StageName = 'Closed Won' group by Quota__c ]){
        amtmap.put((ID)ag.get('Quota__c'), double.valueof(ag.get('SUM')));
    }
    list<Quota__c>Qtalist = new list<Quota__c>();
    for(id iid : QtaIds){
        Quota__c quot = new Quota__c(id=iid); 
        if(amtmap.containskey(iid)){
            quot.Inside_Sales_Roll_up__c = amtmap.get(iid);
        }else{
            quot.Inside_Sales_Roll_up__c = 0;
        } 
        Qtalist.add(quot);       
    } 
    if(Qtalist.size()>0){
        update Qtalist;
    }
}

Below is My OpportunityTrigger
 
trigger OpportunityTrigger on Opportunity (after insert,after update) {
    
    IF(Trigger.isafter && Trigger.isupdate){ 
        OpportunityHelper.MMCountRollUpAmount();
     }
    
}

This is My OpportunityHelper
 
public class OpportunityHelper {
    
    public static Map<Id, Quota__c> QuotaMap =new Map<Id, Quota__c>();
    public static Map<String, List<Opportunity>> OpportunityListMap = new Map<String, List<Opportunity>>();
    public static set<ID>QtaIds = new set<ID>(); 
    
    public static void MMCountRollUpAmount(set<id> QuotaIdSet){
        set<ID>QtaIds = new set<ID>(); 
        if(trigger.isinsert || trigger.isundelete){
            for(opportunity opp : trigger.new){
                QtaIds.add(opp.Quota__c);
            }
        }  
        if(trigger.isdelete){
            for(opportunity opp : trigger.old){
                QtaIds.add(opp.Quota__c);           
            }        
        }
        if(trigger.isupdate){
            for(opportunity opp:trigger.new){
                QtaIds.add(opp.Quota__c);
                if(trigger.oldmap.get(opp.id).Quota__c != opp.Quota__c && trigger.oldmap.get(opp.id).Quota__c != null ){
                    QtaIds.add(trigger.oldmap.get(opp.id).Quota__c);
                }            
            }
        }    
        map<id,double> amtmap = new map<id,double>();
        for(aggregateresult ag : [SELECT SUM(Amount) sum,Quota__c FROM Opportunity where Quota__c in :QtaIds and Closed_Date_Checkbox__c ='True' AND StageName = 'Closed Won' group by Quota__c ]){
            amtmap.put((ID)ag.get('Quota__c'), double.valueof(ag.get('SUM')));
        }
        list<Quota__c>Qtalist = new list<Quota__c>();
        for(id iid : QtaIds){
            Quota__c quot = new Quota__c(id=iid); 
            if(amtmap.containskey(iid)){
                quot.Inside_Sales_Roll_up__c = amtmap.get(iid);
            }else{
                quot.Inside_Sales_Roll_up__c = 0;
            } 
            Qtalist.add(quot);       
        } 
        if(Qtalist.size()>0){
            update Qtalist;
        }
    }
}

TT​​​​​​​
Anil JAdhav 14Anil JAdhav 14
Hi Rithu,

Please find below code. Let me know if face any errors.

Opportunity Trigger
trigger OpportunityTrigger on Opportunity (after delete,after update,after insert,after undelete) {
        OpportunityHelper.MMCountRollUpAmount(trigger.new,trigger.old);
}
OpportunityHelper
public class OpportunityHelper {
    public static void MMCountRollUpAmount(list<Opportunity> newOpps, list<Opportunity> oldOpps){
        set<ID>QtaIds = new set<ID>(); 
        if(trigger.isinsert || trigger.isundelete){
            for(opportunity opp : newOpps){
                QtaIds.add(opp.Quota__c);
            }
        }  
        if(trigger.isdelete){
            for(opportunity opp : oldOpps){
                QtaIds.add(opp.Quota__c);           
            }        
        }
        if(trigger.isupdate){
            for(opportunity opp:newOpps){
                QtaIds.add(opp.Quota__c);
                if(trigger.oldmap.get(opp.id).Quota__c != opp.Quota__c && trigger.oldmap.get(opp.id).Quota__c != null ){
                    QtaIds.add(trigger.oldmap.get(opp.id).Quota__c);
                }            
            }
        }    
        map<id,double> amtmap = new map<id,double>();
        for(aggregateresult ag : [SELECT SUM(Amount) sum,Quota__c FROM Opportunity where Quota__c in :QtaIds and Closed_Date_Checkbox__c ='True' AND StageName = 'Closed Won' group by Quota__c ]){
            amtmap.put((ID)ag.get('Quota__c'), double.valueof(ag.get('SUM')));
        }
        list<Quota__c>Qtalist = new list<Quota__c>();
        for(id iid : QtaIds){
            Quota__c quot = new Quota__c(id=iid); 
            if(amtmap.containskey(iid)){
                quot.Inside_Sales_Roll_up__c = amtmap.get(iid);
            }else{
                quot.Inside_Sales_Roll_up__c = 0;
            } 
            Qtalist.add(quot);       
        } 
        if(Qtalist.size()>0){
            update Qtalist;
        }
    }
}


 
Manju053Manju053
@Anil

Thanks for the Reply, Still i am Getting the same Errors
 
IF(Trigger.isafter && Trigger.isupdate){ 
        OpportunityHelper.MMCountRollUpAmount(trigger.new,trigger.old);
     }
    
}

Error -  Method does not exist or incorrect signature: void MMCountRollUpAmount(List<Opportunity>, List<Opportunity>) from the type OpportunityHelper


 
public static void MMCountRollUpAmount(list<Opportunity> newOpps, list<Opportunity> oldOpps){
        set<ID>QtaIds = new set<ID>(); 
        if(trigger.isinsert || trigger.isundelete){
            for(opportunity opp : newOpps){
                QtaIds.add(opp.Quota__c);
            }
        }  
        if(trigger.isdelete){
            for(opportunity opp : oldOpps){
                QtaIds.add(opp.Quota__c);           
            }        
        }
        if(trigger.isupdate){
            for(opportunity opp:newOpps){
                QtaIds.add(opp.Quota__c);
                if(trigger.oldmap.get(opp.id).Quota__c != opp.Quota__c && trigger.oldmap.get(opp.id).Quota__c != null ){
                    QtaIds.add(trigger.oldmap.get(opp.id).Quota__c);
                }            
            }
        }    
        map<id,double> amtmap = new map<id,double>();
        for(aggregateresult ag : [SELECT SUM(Amount) sum,Quota__c FROM Opportunity where Quota__c in :QtaIds and Closed_Date_Checkbox__c ='True' AND StageName = 'Closed Won' group by Quota__c]){
            amtmap.put((ID)ag.get('Quota__c'), double.valueof(ag.get('SUM')));
        }
        list<Quota__c>Qtalist = new list<Quota__c>();
        for(id iid : QtaIds){
            Quota__c quot = new Quota__c(id=iid); 
            if(amtmap.containskey(iid)){
                quot.Inside_Sales_Roll_up__c = amtmap.get(iid);
            }else{
                quot.Inside_Sales_Roll_up__c = 0;
            } 
            Qtalist.add(quot);       
        } 
        if(Qtalist.size()>0){
            update Qtalist;
        }
    }
}


Errors


where Quota__c in :QtaIds and Closed_Date_Checkbox__c ='True'
                              ^
ERROR at Row:1:Column:80
value of filter criterion for field 'Closed_Date_Checkbox__c' must be of type boolean and should not be enclosed in quotes


User-added image


So my overall Code is
 
Opportunity Trigger

trigger OpportunityTrigger on Opportunity (after delete,after update,after insert,after undelete) {
    
     IF(Trigger.isafter && Trigger.isupdate){
        OpportunityHelper.OppShareInsertUpdate(Trigger.New);
     } 
    
    IF(Trigger.isafter && Trigger.isupdate){ 
        OpportunityHelper.MMCountRollUpAmount(trigger.new,trigger.old);
     }
    
}


OpportunityHelper


public class OpportunityHelper {
    
    public static List<OpportunityShare> csShareList = new List<OpportunityShare>();
    public static List<OpportunityShare> removeShareList = new List<OpportunityShare>();
    public static Map<Id, Quota__c> QuotaMap =new Map<Id, Quota__c>();
    public static Map<String, List<Opportunity>> OpportunityListMap = new Map<String, List<Opportunity>>();
    public static set<ID>QtaIds = new set<ID>(); 
    
    public static void OppShareInsertUpdate(List<Opportunity> Opps){
        csShareList = new List<OpportunityShare>();
        removeShareList = new List<OpportunityShare>();
        Set<Id> OPPids = new Set<Id>();
        for( Opportunity cs : Opps) {
            if( cs.CRM_User__c != NULL ) {
                // Create a new LeadShare object for each Lead where CRM_User__c field is not NULL.
                OpportunityShare csShare = new OpportunityShare();
                // Give Read write access to that user for this particular Lead record.
                csShare.OpportunityAccessLevel = 'Read';
                // Assign Lead Id of Lead record.
                csShare.OpportunityId = cs.id;
                // Assign user id to grant read write access to this particular Lead record.
                csShare.UserOrGroupId = cs.CRM_User__c;
                csShareList.add( csShare );
            }
            if( cs.CRM_User__c == NULL ) {
                OPPids.add( cs.id);
            }
        }
        
        if(OPPids.size()>0){
            removeShareList = [Select id,OpportunityId,UserOrGroupId from OpportunityShare where OpportunityId=:OPPids AND RowCause = 'Manual'];
            if(removeShareList.size()>0){
                delete removeShareList;
            }
        }
        if( csShareList != null && csShareList.size() != 0 ) {
            
            insert csShareList;
            update csShareList;
        }
    }
    public static void MMCountRollUpAmount(list<Opportunity> newOpps, list<Opportunity> oldOpps){
        set<ID>QtaIds = new set<ID>(); 
        if(trigger.isinsert || trigger.isundelete){
            for(opportunity opp : newOpps){
                QtaIds.add(opp.Quota__c);
            }
        }  
        if(trigger.isdelete){
            for(opportunity opp : oldOpps){
                QtaIds.add(opp.Quota__c);           
            }        
        }
        if(trigger.isupdate){
            for(opportunity opp:newOpps){
                QtaIds.add(opp.Quota__c);
                if(trigger.oldmap.get(opp.id).Quota__c != opp.Quota__c && trigger.oldmap.get(opp.id).Quota__c != null ){
                    QtaIds.add(trigger.oldmap.get(opp.id).Quota__c);
                }            
            }
        }    
        map<id,double> amtmap = new map<id,double>();
        for(aggregateresult ag : [SELECT SUM(Amount) sum,Quota__c FROM Opportunity where Quota__c in :QtaIds and Closed_Date_Checkbox__c ='True' AND StageName = 'Closed Won' group by Quota__c]){
            amtmap.put((ID)ag.get('Quota__c'), double.valueof(ag.get('SUM')));
        }
        list<Quota__c>Qtalist = new list<Quota__c>();
        for(id iid : QtaIds){
            Quota__c quot = new Quota__c(id=iid); 
            if(amtmap.containskey(iid)){
                quot.Inside_Sales_Roll_up__c = amtmap.get(iid);
            }else{
                quot.Inside_Sales_Roll_up__c = 0;
            } 
            Qtalist.add(quot);       
        } 
        if(Qtalist.size()>0){
            update Qtalist;
        }
    }
}

 
Anil JAdhav 14Anil JAdhav 14
Hi Rithu,

Please replace 
Closed_Date_Checkbox__c ='True'
with 
Closed_Date_Checkbox__c =True
It will work.