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
Jeff GJeff G 

How to Update the Stage Based on the Probability of an Opportunity?

Hi all,

I want to update the stage of an opportunity based on the completion percentage of a custom 'QQ' object linked to that opportunity.

So far, I have a trigger that automatically updates the standard probability field of the opportunity whenever the QQ is changed.

I have just written another trigger that I want to update the opportunity's stage name: basically, if the standard probability is within a certain range of %, I want the opportunity to be at the corresponding stage 'x'.

It basically consists of a bunch of nested conditionals:

trigger UpdateStage on Opportunity (after update) {
    for(Opportunity {
               if(p.Probability  == 100) {
                    p.StageName = 'Closed Won';
                    //problem: in the QQ, there is no % assoc. with step 6 ('Invoiced') of the Launch phase...
                    //total % can be 100% without having fully closed and won the sale                                 
                else if(p.Probability < 0) {
                    p.StageName = 'Closed Lost';    //will associate a negative value with a closed lost (to distinguish it from warm prospect)
                else if(p.Probability < 100) {
                    if(p.Probability<89) {
                        if(p.Probability<53) {
                            if(p.Probability<32) {
                                if(p.Probability<12) {
                                    if(p.Probability == 0) {
                                        p.StageName = 'Warm Prospect';    //if 0%. stage is in Warm Prospect phase
                                    else p.StageName = 'Discovery';    //if 0-12%, stage is in Discovery phase
                                else p.StageName = 'Qualify';    //if 12-32%, stage is in Qualify phase
                            else p.StageName = 'Solution';    //if 32-53%, stage is in Solution phase
                        else p.StageName = 'Close';    //if 53-89%, stage is in Close phase
                    else p.StageName = 'Launch';    //if 89-100%, stage is in Launch phase

The problem is, whenver I activate this updateStage trigger in conjunction with my probability updating trigger, I get the following error:

Apex trigger FINALUpdateOppProb caused an unexpected exception, contact your administrator: UpdateOppProb: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 006G000000Xk7U6IAJ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateStage: execution of AfterUpdate caused by: System.FinalException: Record is read-only Trigger.UpdateStage: line 29, column 1: []: Trigger.FINALUpdateOppProb: line 46, column 1

Can I not alter the stage name if it is a read-only record? Does anyone know if there are ways around this?

Thanks in advance!
This is because you are trying the update the same record in the after insert/update operation. Did you tried in before update operation? 
Jeff GJeff G

Yes I tried a before update trigger for the stage updates. However, doing this overrides my probability trigger since there are preset Probaility % associated with a given stage.

For example, if my QQ completion is 53%, I want my Opportunity's Probability to be 53% and the stage to be 'Close'. But when my stage update trigger changes the stage to 'Close', the Opportunity Probability changes to 85% because that is the value defined in the 'Close' stage by default.

I originally wanted to remove the linkage between stage and probability, but this is impossible since it is hardcoded in Salesforce. That's why I'm trying to make use of triggers to try to update the stage based on custom probability values.
Hi Jeff - did you ever solve this?  I have a similar issue I'm trying to solve and wondering how this finally worked out... thanks