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
PetyaPetya 

Trigger to update the quote status

Hello

I try to make a trigger on the Quote and set the quote status to "approved"  when  the condition from the opportunity „Opportunity.Approval_Confirmed_c“ is true (the opportunity is approved).  When that happens I could send the quote via email and this is only possible by me, when the quote is approved.

I tried to make this with workflow, but it doesn't work, how I wish. With a workflow the quote status will be updated only when the quote is edited or created, and I need to see the changes after updating the opportunity without editing the quote. An Opportunity can have many quote and this is also missing in my code.


Trigger QuoteTriggers on Quote (after insert, after update) {

List<Quote> listQ = [SELECT Id, Opportunity.Account.Id,
Opportunity.Approval_Confirmed_c FROM Quote WHERE id IN :trigger.New];

 for(Quote q : listQ){
 if (q.Opportunity.Approval_Confirmed_c = True) {   
       q.Status = 'Approved';
  }
   Database.update(listQ);
    }
    
 }
I receive error messege with my code
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger QuoteTriggers caused an unexpected exception, contact your administrator: QuoteTriggers: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0Q0W00000004FdKKAU; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0Q0W00000004FdK) is currently in trigger QuoteTriggers, therefore it cannot recursively update itself: []: Trigger.QuoteTriggers: line 10, column 1
 
Best Answer chosen by Admin (Salesforce Developers) 
sanjdevsanjdev

Hi Try below stuff

 

Trigger QuoteTriggers on Quote (after insert, after update) {
List<Quote> list_updateQuote = new List<Quote>();
List<Quote> listQ = [SELECT Id, Opportunity.Account.Id,
Opportunity.Approval_Confirmed_c FROM Quote WHERE id IN :trigger.New];

for(Quote q : listQ){
if (q.Opportunity.Approval_Confirmed_c = True) {
q.Status = 'Approved';
list_updateQuote.add(q);
}

}
Database.update(list_updateQuote);

}

All Answers

sanjdevsanjdev

Hi Try below stuff

 

Trigger QuoteTriggers on Quote (after insert, after update) {
List<Quote> list_updateQuote = new List<Quote>();
List<Quote> listQ = [SELECT Id, Opportunity.Account.Id,
Opportunity.Approval_Confirmed_c FROM Quote WHERE id IN :trigger.New];

for(Quote q : listQ){
if (q.Opportunity.Approval_Confirmed_c = True) {
q.Status = 'Approved';
list_updateQuote.add(q);
}

}
Database.update(list_updateQuote);

}

This was selected as the best answer
PetyaPetya

it is already done, but many thanks for your solution :)