You need to sign in to do that
Don't have an account?
XIO
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 2:
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; } }
All Answers
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