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

Need help with Opportunity Trigger

I am trying to set up a trigger.  Opportunity has Opportunity Products which are just Products with a relationship to an opportunity.  When a custom field on Product is set a certain way I would like to update a custom field on the opportunity record to say the Product name.  I am having trouble figuring out where to place the trigger and how to write the handler class. 

The trigger will definitly be placed on the Product object. In the class you will need to query all the opportunities which have the current Product associated to the them Select Id, customfield__c from opportunity where Product in // will contain the products which have been modified Post this you will need to edit the custom field of all the opportunities queried above according to the requirement. Please let me know in case u need more help

Can you expalin why the trigger needs to be placed on Product?  It seems to me that the trigger needs to be on Opportunity-Product (OpportunityLineItem) Since thats whats being created or updated when you add a product to an opportunity.  


Otherwise this is the code I have now... It compiles fine but nothing happens when I expect the trigger to fire...


public with sharing class OpportunityModelNameTriggerHandler {

public void OnAfterInsert(List<OpportunityLineItem> newRecords){


public void OnAfterUpdate(List<OpportunityLineItem> oldRecords,
List<OpportunityLineItem> updatedRecords, Map<ID, OpportunityLineItem> oldMap,
Map<ID, OpportunityLineItem> newMap){

private void updateOpportunity(List<OpportunityLineItem> newRecords) {

 Map<ID,String> productNames = new Map<ID,String>();
List<Opportunity> opps;
List<Product2> prods;
Map<ID,Opportunity> opportunities = new Map<ID,Opportunity>();
Map<ID,PriceBookEntry> pbIDs = new Map<ID,PriceBookEntry>();
Map<ID,Product2> products = new Map<ID,Product2>();

for (OpportunityLineItem oli : newRecords) {
prods = [select id from Product2 where id IN :pbIDs.keyset()];

for(Product2 prod : prods)
if(prod.Option_Category__c == 'Model')

opps = [select id from Opportunity
where id IN :opportunities.keyset()];

for (Opportunity opp : opps)
opp.model__c = productNames.get(;
// commit the records
update opps;


PS... is there anyway I can debug this in the browser?   Break points or at least print statements?  Current value of variables during runtime?


bump?edit with solution


Set<Id> CurrentOppId = new Set<Id>();
for (OpportunityLineItem OppLnItem : newRecords){

// Create List of One Opportunity Id to Update
List<Opportunity> OppId =
[Select o.Id from Opportunity o
where o.Id in: CurrentOppId];


List<OpportunityLineItem> relatedOLIs =
[Select oli.Id,, PricebookEntry.Product2.Option_Category__c from OpportunityLineItem oli
where oli.OpportunityId in: CurrentOppId];

for (Opportunity opp2 : OppId){

for (OpportunityLineItem oli : relatedOLIs){
if(oli.PricebookEntry.Product2.Option_Category__c == 'Model')
opp2.model__c =;
update OppID;


You are correct to have the trigger on oli. I had meant the same!!!. You can use system.debug(); statements within the code and then use system log to check for the debugging statements. Could you also paste the code of the trigger you are using....