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
NevDevNevDev 

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 
trigger OpportunityLineItemtrigger on OpportunityLineItem (after insert, After update) {

if(Trigger.IsInsert || Trigger.IsUpdate)
{
OpportunityLineItemtriggerHandler.LogicAfterInsertORupdate(Trigger.New);
}


}



//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
    LstOfopp.add(o);
}

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.

Thanks,
Shamsi