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
Victor PVictor P 

Trigger that writes all Products in a custom field at Opportunity object level

Hi all,

I'm trying to create a trigger on the Opportunity object to write all Products of said Opportunity in a custom field.

I found how to do this in an Apex class but it looks like Apex triggers are using a different language? 
 
trigger OpportunityConcatenation on Opportunity (before insert, before update, after insert, after update, before delete)

{
    var opp = new sforce.SObject("Opportunity");
opp.Id = '{!Opportunity.Id}';

result = sforce.connection.query("Select PricebookEntry.Product2.Name, Quantity, TotalPrice From OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}' and (NOT Name like '%Discount%')");
records = result.getArray("records");

var strProductNames = '';
for(var i=0; i<records.length ; i++){
 strProductNames += 'PRODUCT NAME: ' + records[i].PricebookEntry.Product2.Name + ' --- QUANTITY: ' + records[i].Quantity + ' --- TOTAL PRICE: $ ' + records[i].TotalPrice +',\n';
}

if(strProductNames.length>0){
 strProductNames = strProductNames.substring(0,strProductNames.length-2);
}
opp.Samples_Sent__c = strProductNames;

sforce.connection.update([opp]);
}

 
Ravi Dutt SharmaRavi Dutt Sharma
Hi Victor,

You should consider writing a trigger on OpportunityLineItem(OLI) instead of Opportunity. Whenever a product is added to an opportunity, a record is created in the OLI object. You should write a trigger on insert of OLI which will update the custom field present on parent opportunity record. OLI has lookup to Product, Pricebookentry and Opportunity, so you have all the information you need to update your custom field.
Victor PVictor P
Hi Ravi, what if I want to specifically trigger this when an Opportunity is updated? If an Opportunity already exists and I update something, that field will be empty because it will only trigger on OLI object updates. Additionally, I write all these fields in the Opportunity object because that's were the second trigger I use to export this field is used. It sends all Opportunity details along with the custom field of all Products in an outbound message.

What's the downside? Not seeing when a new Product is created without updating the Opportunity? It sounds like way more complicated if I put it in the OpportunityLineItem object because I would have to first check if the Product already exists in the list and only add it if it isn't. Then I'd have to create something to remove Products that have been removed, etc.
 
Ravi Dutt SharmaRavi Dutt Sharma
If you write a trigger on Opportunity, then how will you handle the screnario when a new product is added to your opportunity? when a product gets deleted from opportunity? You will not get the handle whenever a product gets added/updated or deleted if you write your trigger on Opportunity.
 
Ravi Dutt SharmaRavi Dutt Sharma
1. Write a trigger on OLI. In the trigger you will get the opportunity ID because OLI is associated with an opportunity.
2. With that opportunity id, query all the OLI already present under that opportunity. Loop through the result, each result item will have the product name. Keep concatinating the product names in a variable in the loop.
3. Replace the result in Samples_Sent__c field present on Opportunity.