+ Start a Discussion

Adding a validation to an existing trigger

I have an existing trigger which is running on the Opportunity object. I need to add a validation rule which 
1. Updates the Approval_Status__c field on the Opportunity to a null value if a related Product is modified or a new Product is added to the Opportunity.
2. Prevent a user from moving the stage backwards. 
Best Answer chosen by NevDev
Shamsi 110Shamsi 110
1) This can be achieved something like this

trigger OpportunityLineItemtrigger on OpportunityLineItem (after insert, After update) {

if(Trigger.IsInsert || Trigger.IsUpdate)


//TRigger Handler Class
public class OpportunityLineItemtriggerHandler{

public static void LogicAfterInsertORupdate(list<OpportunityLineItem> LstOfOLI){

List<Opportunity> LstOfopp = new List<Opportunity>();

for(OpportunityLineItem oli :LstOfOLI )
    Opportunity o = new Opportunity();
    o.Approval_Status__c = '';
    o.id  = oli.oli

if(LstOfopp.size() >0)
 update LstOfopp;


2) You can may create a validation rule look at ischanges or isNew functions , that might help you.

Kindly mark this answer, if it helps you.