+ Start a Discussion
XIOXIO 

Merge 2 Triggers into 1 Trigger - Need HELP!

Hello Developers!

I need help merging the two (2) triggers below into one (1) trigger. Any assistance is greatly appreciated. Thank you!

Trigger 1:
Trigger OpptyProducts_Spring on Opportunity (before update) {

list<opportunity> sl = trigger.new;

list<opportunityLineItem> slnu = new list<opportunityLineItem>([select id ,product2.name, opportunityId from opportunitylineitem where IsSpring__c = TRUE AND  opportunityId =: trigger.new[0].id]);

string productName='';

for(opportunityLineItem opp : slnu){

 productName += opp.product2.name + '\n'; // + operator for concatenation.

}

for(Opportunity opp : trigger.new){

 opp.Spring_Products__c = productName;


}


}

Trigger 2:
Trigger OpptyProducts_Fall on Opportunity (before update) {

list<opportunity> sl = trigger.new;

list<opportunityLineItem> slnu = new list<opportunityLineItem>([select id ,product2.name, opportunityId from opportunitylineitem where IsFall__c = TRUE AND  opportunityId =: trigger.new[0].id]);

string productName='';

for(opportunityLineItem opp : slnu){

 productName += opp.product2.name + '\n'; 

}

for(Opportunity opp : trigger.new){

 opp.Fall_Products__c = productName;


}


}

 
Best Answer chosen by XIO
IanDoneganIanDonegan
Trigger OpptyProducts on Opportunity (before update) {
	for(Opportunity opp : trigger.new){
        list<opportunityLineItem> slnu = new list<opportunityLineItem>([select id ,product2.name, opportunityId from opportunitylineitem where (IsSpring__c = TRUE OR isFall__c = TRUE) AND  opportunityId =: opp.Id]);

        string productName_spring='';
        string productName_fall='';

        for(opportunityLineItem item : slnu){
            if IsSpring__c {
                productName_spring += item.product2.name + '\n';
            }
            if isFall__c {
                productName_fall += item.product2.name + '\n';
            }
        }

		opp.Spring_Products__c = productName_spring;
		opp.Fall_Products__c = productName_fall;
	}
}
I would try something along the lines of this.

All Answers

IanDoneganIanDonegan
Trigger OpptyProducts on Opportunity (before update) {
	for(Opportunity opp : trigger.new){
        list<opportunityLineItem> slnu = new list<opportunityLineItem>([select id ,product2.name, opportunityId from opportunitylineitem where (IsSpring__c = TRUE OR isFall__c = TRUE) AND  opportunityId =: opp.Id]);

        string productName_spring='';
        string productName_fall='';

        for(opportunityLineItem item : slnu){
            if IsSpring__c {
                productName_spring += item.product2.name + '\n';
            }
            if isFall__c {
                productName_fall += item.product2.name + '\n';
            }
        }

		opp.Spring_Products__c = productName_spring;
		opp.Fall_Products__c = productName_fall;
	}
}
I would try something along the lines of this.
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Harmens,
Try this code and change according to your requirement:
Trigger OpptyProducts_Spring_Fall on Opportunity (before update) {
    
    list<opportunityLineItem> slnu = new list<opportunityLineItem>([SELECT Id, Product2.name, OpportunityId FROM Opportunitylineitem WHERE IsSpring__c = TRUE AND  OpportunityId =: trigger.new[0].id]);
    list<opportunityLineItem> slnuNext = new list<opportunityLineItem>([select id ,product2.name, opportunityId from opportunitylineitem where IsFall__c = TRUE AND  opportunityId =: trigger.new[0].id]);

    string productName = '';
    string productNameNext = '';
    
    for(opportunityLineItem opp : slnu) {
        
        productName += opp.product2.name + '\n'; // + operator for concatenation.
        
    }
    
    for(opportunityLineItem opp : slnuNext){
        
        productNameNext += opp.product2.name + '\n'; 
        
    }

    
    for(Opportunity opp : trigger.new){
        opp.Spring_Products__c = productName;
    }
    
    for(Opportunity opp : trigger.new){
        opp.Fall_Products__c = productNameNext;
    }
    
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
XIOXIO
Thank you the help IanDonegan and Ajay!!