+ Start a Discussion
John BraunJohn Braun 

Trigger to update the quote status based on checkbox on opportunity

Hello,

 

I was curious if I could get assistance with creating a trigger that would update the quote status field for the synced quote based on if a checkbox on the opportunity is true or false.

 

If the opportunity checkbox is "TRUE", the trigger would update the quote status to "Pending RFP".

 

I would only have a single quote related to the opportunity at any point in time and it would always be "synced" using the native salesforce.com quote sync functionality. Would anyone be willing to assist? Thanks so much for any advice.

Best Answer chosen by Admin (Salesforce Developers) 
TheDoctorTheDoctor

Assuming you want this to trigger on insert/edit of the Opportunity, try something like this:

 

trigger updateQuoteStatus on Opportunity (after insert, after update){

    Quote q = [select status from Quote where Opportunity = :trigger.new[0].id and IsSyncing = true];

    for(Opportunity opp : trigger.new){

        if(opp.Opportunity.Checkbox__c == true){

            q.status = 'Pending RFP';
        }
    }

    update q;
}

 

All Answers

TheDoctorTheDoctor

Assuming you want this to occur on insert/edit of the Quote, try something like this:

 

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

    List<Quote> qList = new List<Quote>();

    for(Quote q : trigger.new){

        if(q.Opportunity.Checkbox__c == true){

            q.status = 'Pending RFP';
            qList.add(q);
        }
    }

    if(qList.size() > 0){
        update qList;
    }
}

 

TheDoctorTheDoctor

Assuming you want this to trigger on insert/edit of the Opportunity, try something like this:

 

trigger updateQuoteStatus on Opportunity (after insert, after update){

    Quote q = [select status from Quote where Opportunity = :trigger.new[0].id and IsSyncing = true];

    for(Opportunity opp : trigger.new){

        if(opp.Opportunity.Checkbox__c == true){

            q.status = 'Pending RFP';
        }
    }

    update q;
}

 

This was selected as the best answer
John BraunJohn Braun

Thank you for the reply!!

 

I'm still getting the following error:

 


Error: Compile Error: Comparison arguments must be compatible types: Decimal, Boolean at line 7 column 12

 

 

Using this code:

 

 

trigger updateQuoteStatus on Opportunity (after insert, after update){

Quote q = [select status from Quote where Opportunityid = :trigger.new[0].id and IsSyncing = true];

for(Opportunity opp : trigger.new){

if(opp.Resubmit_Quote__c == true){

q.status = 'Pending RFP';
}
}

update q;
}

John BraunJohn Braun

Nevermind, I was referencing the wrong field, this worked great, thank you!!

John BraunJohn Braun

TheDoctor,

 

The functionality is working as expected, however, it seems that when this trigger is active I cannot create an opportunity record from scratch. When I attempt to create an opportunity, I get the following error:

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger updateQuoteStatus caused an unexpected exception, contact your administrator: updateQuoteStatus: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.updateQuoteStatus: line 3, column 1

 

Any idea what may be causing this? Thanks so much for your help, I'm getting really close to what I'd like to accomplish.

John BraunJohn Braun
BTW - the code you suggested that I'm working with is right here:

trigger updateQuoteStatus on Opportunity (after insert, after update){

Quote q = [select status from Quote where Opportunityid = :trigger.new[0].id and IsSyncing = true];

for(Opportunity opp : trigger.new){

if(opp.Quote_Re_submitted__c == true){

q.status = 'Pending RFP';
}
}

update q;
}
TheDoctorTheDoctor

Thinking about this a little more, you could never have a Quote before you insert an Opp, so give this a try:

 

trigger updateQuoteStatus on Opportunity (after update){

    Quote q = [select status from Quote where Opportunityid = :trigger.new[0].id and IsSyncing = true];

    if(q != null){
    
        for(Opportunity opp : trigger.new){

            if(opp.Quote_Re_submitted__c == true){

                q.status = 'Pending RFP';
            }
        }

        update q;
    }
}

 

John BraunJohn Braun

TheDoctor,

 

Thanks for continuing to assist me with this, it looks like with the new revised code, I still get the following error when creating an opportunity:

 

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger updateQuoteStatus caused an unexpected exception, contact your administrator: updateQuoteStatus: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.updateQuoteStatus: line 3, column 1". 

John BraunJohn Braun

TheDoctor,

 

I gave your most recent revision a try:

 

trigger updateQuoteStatus on Opportunity (after insert, after update){ Quote q = [select status from Quote where Opportunityid = :trigger.new[0].id and IsSyncing = true]; if(q != null){ for(Opportunity opp : trigger.new){ if(opp.Quote_Re_submitted__c == true){ q.status = 'Pending RFP'; } } update q; } }

 

And I get this upon attempting to create an opportunity:

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger updateQuoteStatus caused an unexpected exception, contact your administrator: updateQuoteStatus: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.updateQuoteStatus: line 1, column 1

 

Again, thanks so much for your help on this.

TheDoctorTheDoctor

You didn't remove the 'after insert' from the first line. ;)