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
KavitaKavita 

Help with writing a trigger to update field

How do I write a trigger for the opportunity product object that updates the "sales price" field every time the "discount field" or the "Qty" field is changed?

 

Please help.

 

Best Answer chosen by Admin (Salesforce Developers) 
jpwagnerjpwagner

really?

 

you're not even going to try?

 

ok, here's a little help:

 

for(OpportunityLineItem oli : Trigger.new){

  OpportunityLineItem oldoli = System.Trigger.oldmap.get(oli.Id); 

  if(oli.Quantity != oldoli.Quantity){

//    oli.UnitPrice = something;

  } 

}

 

before update *only*.

"why?" you ask...I'll leave that to you as an exercise.

All Answers

jpwagnerjpwagner

really?

 

you're not even going to try?

 

ok, here's a little help:

 

for(OpportunityLineItem oli : Trigger.new){

  OpportunityLineItem oldoli = System.Trigger.oldmap.get(oli.Id); 

  if(oli.Quantity != oldoli.Quantity){

//    oli.UnitPrice = something;

  } 

}

 

before update *only*.

"why?" you ask...I'll leave that to you as an exercise.

This was selected as the best answer
KavitaKavita
:) Thanks so much. I will work with this.