+ Start a Discussion
Jim MontgomeryJim Montgomery 

convert 3 apex triggers to single apex class

I have 3 triggers like the one in this class I need add into this class. Not sure how to add them in.

public class HasFlagsTriggerHandler {
    
     public static void HasTax(List<Apttus_Proposal__Proposal_line_item__c> props){
        Map<ID, Apttus_Proposal__Proposal__c> parentProposal = new Map<ID, Apttus_Proposal__Proposal__c>();
    List<Id> listIds = new List<Id>();
    List<Id> LineItems = new List<Id>();
    

    for (Apttus_Proposal__Proposal_Line_Item__c childObj : props) {
        listIds.add(childObj.Apttus_Proposal__Proposal__c);
        LineItems.add(childObj.Id);
    }
parentProposal = new Map<Id, Apttus_Proposal__Proposal__c>([SELECT id,Has_Tax_Lines__c,(Select Id from R00N70000001yUfBEAU__r where Product_Family__c = 'Tax'LIMIT 1) FROM Apttus_Proposal__Proposal__c WHERE ID IN :listIds]);
    
    for (Apttus_Proposal__Proposal_Line_Item__c ProposalLineItem: props){
         Apttus_Proposal__Proposal__c myParentProposal = parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c);
        if(parentProposal.containsKey(ProposalLineItem.Apttus_Proposal__Proposal__c) && parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c).R00N70000001yUfBEAU__r.size() > 0)
        {
            myParentProposal.Has_Tax_Lines__c = true;
        }
        else
        {
            myParentProposal.Has_Tax_Lines__c = false ;
        }
    }
    update parentProposal.values();
    }
 
v varaprasadv varaprasad
Hi Jim,


Check once below sample code : 
triiger ex1 on account(after insert){
    class.method(trigger.new);
}

triiger ex2 on account(after update){
    class.method(trigger.new);
}

triiger ex3 on account(Before insert){
    class.method(trigger.new);
}

=====Instead of 3 triggers you can add alevents in one trigger like below:

triiger singletrigger on account(Before insert,after insert,after update){
  if(trigger.isBefore && trigger.isinsert){   
   class.method(trigger.new);
   }
   
    if(trigger.isAfter && trigger.isinsert){   
   class.method(trigger.new);
   }
   
    if(trigger.isAfter && trigger.isupdate){   
   class.method(trigger.new);
   }
}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.


Thanks
Varaprasad
Salesforce Freelance Consultant/Developer/Administrator
@For Salesforce Project Support: varaprasad4sfdc@gmail.com


Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1


 
Jim MontgomeryJim Montgomery
Let me clarify. I need all of these routines to run from a single call from a trigger, with out having a line for each routine in the trigger.

public class HasFlagsTriggerHandler {    
     public static void HasTax(List<Apttus_Proposal__Proposal_line_item__c> props){
        Map<ID, Apttus_Proposal__Proposal__c> parentProposal = new Map<ID, Apttus_Proposal__Proposal__c>();
    List<Id> listIds = new List<Id>();
    List<Id> LineItems = new List<Id>();    

    for (Apttus_Proposal__Proposal_Line_Item__c childObj : props) {
        listIds.add(childObj.Apttus_Proposal__Proposal__c);
        LineItems.add(childObj.Id);
    }
parentProposal = new Map<Id, Apttus_Proposal__Proposal__c>([SELECT id,Has_Tax_Lines__c,(Select Id from R00N70000001yUfBEAU__r where Product_Family__c = 'Tax'LIMIT 1) FROM Apttus_Proposal__Proposal__c WHERE ID IN :listIds]);
    
    for (Apttus_Proposal__Proposal_Line_Item__c ProposalLineItem: props){
         Apttus_Proposal__Proposal__c myParentProposal = parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c);
        if(parentProposal.containsKey(ProposalLineItem.Apttus_Proposal__Proposal__c) && parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c).R00N70000001yUfBEAU__r.size() > 0)
        {
            myParentProposal.Has_Tax_Lines__c = true;
        }
        else
        {
            myParentProposal.Has_Tax_Lines__c = false ;
        }
    }
    update parentProposal.values();
    }
public static void HasDataAxcess(List<Apttus_Proposal__Proposal_line_item__c> props){
        Map<ID, Apttus_Proposal__Proposal__c> parentProposal = new Map<ID, Apttus_Proposal__Proposal__c>();
    List<Id> listIds = new List<Id>();
    List<Id> LineItems = new List<Id>();    

    for (Apttus_Proposal__Proposal_Line_Item__c childObj : props) {
        listIds.add(childObj.Apttus_Proposal__Proposal__c);
        LineItems.add(childObj.Id);
    }
parentProposal = new Map<Id, Apttus_Proposal__Proposal__c>([SELECT id,Has_Tax_Lines__c,(Select Id from R00N70000001yUfBEAU__r where Product_Family__c = 'Data Axcess Utility'LIMIT 1) FROM Apttus_Proposal__Proposal__c WHERE ID IN :listIds]);
    
    for (Apttus_Proposal__Proposal_Line_Item__c ProposalLineItem: props){
         Apttus_Proposal__Proposal__c myParentProposal = parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c);
        if(parentProposal.containsKey(ProposalLineItem.Apttus_Proposal__Proposal__c) && parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c).R00N70000001yUfBEAU__r.size() > 0)
        {
            myParentProposal.Has_data_axcess_utility__c = true;
        }
        else
        {
            myParentProposal.Has_Data_Axcess_Utility__c = false ;
        }
    }
    update parentProposal.values();
    }
    public static void HasSitebuilder(List<Apttus_Proposal__Proposal_line_item__c> props){
        Map<ID, Apttus_Proposal__Proposal__c> parentProposal = new Map<ID, Apttus_Proposal__Proposal__c>();
    List<Id> listIds = new List<Id>();
    List<Id> LineItems = new List<Id>();
    

    for (Apttus_Proposal__Proposal_Line_Item__c childObj : props) {
        listIds.add(childObj.Apttus_Proposal__Proposal__c);
        LineItems.add(childObj.Id);
    }
parentProposal = new Map<Id, Apttus_Proposal__Proposal__c>([SELECT id,Has_Tax_Lines__c,(Select Id from R00N70000001yUfBEAU__r where Product_Family__c = 'Site builder'LIMIT 1) FROM Apttus_Proposal__Proposal__c WHERE ID IN :listIds]);
    
    for (Apttus_Proposal__Proposal_Line_Item__c ProposalLineItem: props){
         Apttus_Proposal__Proposal__c myParentProposal = parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c);
        if(parentProposal.containsKey(ProposalLineItem.Apttus_Proposal__Proposal__c) && parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c).R00N70000001yUfBEAU__r.size() > 0)
        {
            myParentProposal.Has_site_builder__c = true;
        }
        else
        {
            myParentProposal.Has_site_Builder__c = false ;
        }
    }
    update parentProposal.values();
    }
}

trigger
trigger HasFlags on Apttus_Proposal__Proposal_Line_Item__c (after insert, after update) {
HasFlagsTriggerHandler.HasTax(trigger.new);
HasFlagsTriggerHandler.Hassitebuilder(trigger.new);
}

Would like all parts of the class to run from one line in the trigger.