+ Start a Discussion
PetyaPetya 

Trigger to update the quote status

Hi 

I try to make my first trigger, that should update the Quote Satus to "Approved", when the Opportunity is approved. Here the code:

trigger OppTriggers on Opportunity (after update) {
or(Opportunity opp: Trigger.new){
List<Quote> listQ = [SELECT Id, Opportunity.Account.Id,
Opportunity.TEM_Approval_Confirmed_RD__c FROM Quote WHERE id =: Quote.Id];
for(Quote q : listQ){
Quote.Status = "Approved";
}
update listQ;
 }

Any Suggestions would be appreciated 

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Mani PenumarthiMani Penumarthi

hai i worked this i just wrote the sample one.

replace the foeld name as u want ..

this is working fine and please let me know once u got executed and ask doubts if u have? or else provide ur email address. so that i can help u .

 

here is your code

 

trigger quote_update on Quote (after update) {

Set<String> quote_ref=new Set<String>();
for(Quote qq : Trigger.new)
{
if(qq.Status=='Approved')
quote_ref.add(qq.OpportunityId);
}

for(Opportunity oo : [select id, StageName from Opportunity where id in: quote_ref])
{
oo.StageName='Closed Won';
update oo;
}


}

 

 

All Answers

JBabuJBabu

Hi,

 

Please don't place any SELECT clauses (and also DMLs) inside FOR loop.

You can have the below line above the the for loop.

 

List<Quote> listQ = [SELECT Id, Opportunity.Account.Id,
Opportunity.TEM_Approval_Confirmed_RD__c FROM Quote WHERE id =: Quote.Id];

 

I think you didnot paste the complete code as I don't see  any way Quote.Id in the select clause getting populated.(or having value)

 

Thanks,

JBabu.

PetyaPetya

Hi and many thanks :)

here is my code with correction:

 

trigger QuoteTriggers on Quote (after update) {
List<Quote> listQ = [SELECT Id, Opportunity.Account.Id,
Opportunity.TEM_Approval_Confirmed_RD__c FROM Quote WHERE id =: Quote.Id];

for(Opportunity opp: Trigger.new){

for(Quote q : listQ){
Quote.Status = 'Approved';
}
update listQ;
} }

 

I need the Trigger on the Quote Object ofcourse to get the quoteID. But it still doesnt work, 

"Invalid bind expression type of Schema.SObjectField for column of type Id on line 3"... 

 

JBabuJBabu

Hi,

 

Please use the below code:

 

trigger QuoteTriggers on Quote (after update) {

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


for(Quote q : listQ){
  q.Status = 'Approved';
}
update listQ;

}

 

Also I dont see any logic in your code that this quote approved needs to be done for related opportunity approved records. If you want that condition you need to write logic for that one.

 

Thanks,

JBabu.

PetyaPetya

Thank you for your help

the idea was to check the condition „Opportunity.Approval_Confirmed_c,“ when the condition is true, the opportunity is approved, and then the quote status should be set to approved, because 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.

 

Thanks

 

 

JBabuJBabu

So did the above suggested code work?

PetyaPetya

yes, it works :) 

but I think, I must insert an If -Condition

 

if(Opportunity.TEM_Approval_Confirmed_RD__c=True){

for(Quote q : listQ){
q.Status = 'Approved';
}
update listQ;
}
 

and many quotes can be defined to an Opportunity, I must define an area.             

 

PetyaPetya

the code doesnt work, i can compile it but, if I set the Opportunity to approved, the quote status does not changed, and if I open the quote and try to edit and save it, I receive Error Message from the trigger...

JBabuJBabu

Hi,

 

What is the error message and can you share the complete code which you used?

 

Thanks,

JBabu.

PetyaPetya

Hi 

 

I have this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14

trigger QuoteTriggers on Quote (before update) {

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

 for(Quote q : listQ){
 if (q.Opportunity.TEM_Approval_Confirmed_RD__c = True) {            
   q.Status = 'Approved';
  }
    update listQ;
    }
    
 }

 

this is the error message

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 AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0Q0W00000004FdKKAU; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, QuoteTriggers: maximum trigger depth exceeded Quote trigger event AfterUpdate for [0Q0W00000004FdK] Quote trigger event AfterUpdate for [0Q0W00000004FdK] Quote trigger event AfterUpdate for [0Q0W00000004FdK] 

 

Thanks

Mani PenumarthiMani Penumarthi

hi,

remove the if condition and place that in the select like

select your query where Opportunity.TEM_Approval_Confirmed_RD__c=True limti 1];

 

now try this,

PetyaPetya

hi and thanks :) I still receive an errormessage... 

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

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

 for(Quote q : listQ){
 
       q.Status = 'Approved';
   }
   Database.update(listQ);
 }
Error Message after editing the Quote and saving:
Apex trigger QuoteTriggers caused an unexpected exception, contact your administrator: QuoteTriggers: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0Q0W00000004FdKKAU; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, QuoteTriggers: maximum trigger depth exceeded Quote trigger event AfterUpdate for [0Q0W00000004FdK] Quote trigger event AfterUpdate for [0Q0W00000004FdK] Quote trigger event AfterUpdate for [0Q0W00000004FdK] Quote trigger event AfterUpdate for [0Q0W00000004FdK] Quote trigger event AfterUpdate for [0Q0W00000004FdK] Quote trigger event AfterUpdate for [0Q0W00000004FdK] Quote trigger event AfterUpdate for [0Q0W00000004FdK] Quote trigger event AfterUpdate
Mani PenumarthiMani Penumarthi

hai i worked this i just wrote the sample one.

replace the foeld name as u want ..

this is working fine and please let me know once u got executed and ask doubts if u have? or else provide ur email address. so that i can help u .

 

here is your code

 

trigger quote_update on Quote (after update) {

Set<String> quote_ref=new Set<String>();
for(Quote qq : Trigger.new)
{
if(qq.Status=='Approved')
quote_ref.add(qq.OpportunityId);
}

for(Opportunity oo : [select id, StageName from Opportunity where id in: quote_ref])
{
oo.StageName='Closed Won';
update oo;
}


}

 

 

This was selected as the best answer