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
NikiG22NikiG22 

Trigger - Update Fields

Hello - I am trying to update fields on thje opportunitylineitem from the Product whe a rep selects a specific product.

 

I have the following code but its not updating? Please help!

 

trigger UpdateOpportunityProductFields on OpportunityLineItem (before insert, before update){ 
 {
    for(OpportunityLineItem myOLI:trigger.new) {            

       if(myOLI.Product_Family__c == 'Resume Search') { 
                        
         myOLI.Views__c = myOLI.PriceBookEntry.Product2.Views__c;
         myOLI.Term_Days__c = myOLI.PriceBookEntry.Product2.Term_Days__c;
         myOLI.Daily_View_Limit__c = myOLI.PriceBookEntry.Product2.Daily_View_Limit__c;               
       }

    }
}
}

 Cheers,

Niki

Best Answer chosen by Admin (Salesforce Developers) 
NikiG22NikiG22

thank you I did get it to work i edited this  

 

if(myOLI.PriceBookEntry.Product2.Family == 'Resume Search')

 

trigger UpdateOpportunityProductFields on OpportunityLineItem (before insert, before update){ 
 {
    for(OpportunityLineItem myOLI:trigger.new) {            

       if(myOLI.PriceBookEntry.Product2.Family == 'Resume Search') { 
                        
         myOLI.Views__c = myOLI.PriceBookEntry.Product2.Views__c;
         myOLI.Term_Days__c = myOLI.PriceBookEntry.Product2.Term_Days__c;
         myOLI.Daily_View_Limit__c = myOLI.PriceBookEntry.Product2.Daily_View_Limit__c; 
         myOLI.Quantity = 1;               
       }

    }
}
}

 Thanks for your help,

Niki

All Answers

MelliottMelliott

I believe you have to do and update statement outside of the if statement. 

Alex.AcostaAlex.Acosta

Debug Log is your friend, make sure you're comparing the values you are expecting within your if statement and what is really being stored.

Chamil MadusankaChamil Madusanka

Hi,

 

@MIlliote

You cannot update any object within the same object's trigger(specially before insert and before update). If you do so you will get following error;

 

System.SObjectException: DML statment cannot operate on trigger.new or trigger.old

 

@ NikiG22

Your code is ok. but you are saying that record is not updating. There will be few reasons.

  1. Check whether you have records with Product_Family__c with "Resume Search"
  2. sometime any other exception can be thrown in the trigger

Therefore, check use the debug logs as Alex said.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

NikiG22NikiG22

thank you I did get it to work i edited this  

 

if(myOLI.PriceBookEntry.Product2.Family == 'Resume Search')

 

trigger UpdateOpportunityProductFields on OpportunityLineItem (before insert, before update){ 
 {
    for(OpportunityLineItem myOLI:trigger.new) {            

       if(myOLI.PriceBookEntry.Product2.Family == 'Resume Search') { 
                        
         myOLI.Views__c = myOLI.PriceBookEntry.Product2.Views__c;
         myOLI.Term_Days__c = myOLI.PriceBookEntry.Product2.Term_Days__c;
         myOLI.Daily_View_Limit__c = myOLI.PriceBookEntry.Product2.Daily_View_Limit__c; 
         myOLI.Quantity = 1;               
       }

    }
}
}

 Thanks for your help,

Niki

This was selected as the best answer