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
UrvikUrvik 

Roll Up Opportunity Product Name up to Opportunity

I'm trying to display opportunity product names to opportunity custom field. The trigger is working fine but it displays only last record name. eg. If I have 4 products related to opportunity, it display 4th product name and not display the first three. I would like to display each product name with ',' in between. here is my code. Any idea what need to ​be added in order to display all related products name.
Trigger OppProdLine on Opportunity (before update) {

list<opportunity> sl = trigger.new;

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

string productName='';

for(opportunityLineItem opp : slnu){

 productName = opp.product2.name;

}

for(Opportunity opp : trigger.new){

 opp.Opportunity_Prod__c = productName;


}


}
Deepak Kumar 138Deepak Kumar 138

The concatenation operator is missing at line no 11. Below is the corrected code.

Trigger OppProdLine on Opportunity (before update) {

list<opportunity> sl = trigger.new;

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

string productName='';

for(opportunityLineItem opp : slnu){

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

}

for(Opportunity opp : trigger.new){

 opp.Opportunity_Prod__c = productName;


}


}
Note - I see your trigger is no bulkified which is in best coding guideline from SFDC.
UrvikUrvik
Thanks Deepak! What do I need to add in order to bulkify this trigger. I really appreciate your help.