• JenD
  • NEWBIE
  • 0 Points
  • Member since 2005

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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 p:Trigger.new) {
        
               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!
-Jeff
  • February 11, 2015
  • Like
  • 0