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 

Trigger Updates Opportunity StageName in Practice, But Not in Test Class

Hi all,

I am trying to update the probability and stage name fields for my Opportunity based on the completion of a custom object 'QQ'. My QQ and Opportunity are in a master-detail relationship, and I've rolled up the QQ total % to the Opportunity object.

To go about this, I've written a trigger that compares the QQ roll up percentage with the Opportunity's probablity percentage. If they are different, I change the Opportunity Probability to that of the QQ percentage, and I adjust the Opportunity's stage field based on certain ranges of percentages. For example, if the QQ is 32-52% complete, I change the Opportunity's stage to 'Solution'.

So long as the QQ Roll Up field populates on the Opportunity layout, this trigger works fine in practice. However, when writing a test class for the trigger, the stage field of my opportunity object never changes from the initial stage I declared it as having. Thus I always get an assertion error: System.AssertException: Assertion Failed: Expected: Solution, Actual: Warm Prospect

My code is shown below:
(1) Trigger
trigger NOVAUpdateOppProb on Opportunity (before update) {

    Map<Id,Decimal>OppRecordMap=new Map<Id,Decimal>();     //To capture the Opp records that had any changes in QQ Rollup

    for(Opportunity OppRecord : Trigger.new) {

       //here I update the Opportunity's stage (if applicable):
        if(OppRecord.QQ_Roll_Up__c == 100) {
                 OppRecord.StageName = 'Closed Won';                       
            }       
            else if(OppRecord.QQ_Roll_Up__c < 0) {
                 OppRecord.StageName = 'Closed Lost';  
            }
                
            else if(OppRecord.QQ_Roll_Up__c < 100) {
                    
                 if(OppRecord.QQ_Roll_Up__c<89) {
                    
                      if(OppRecord.QQ_Roll_Up__c<53) {
                         
                           if(OppRecord.QQ_Roll_Up__c<32) {
                                
                                if(OppRecord.QQ_Roll_Up__c<12) {
                                
                                     if(OppRecord.QQ_Roll_Up__c == 0) {
                                         OppRecord.StageName = 'Warm Prospect';    //if 0%. stage is in Warm Prospect phase
                                     }
                                     else OppRecord.StageName = 'Discovery';    //if 1-11%, stage is in Discovery phase
                                }
                                else OppRecord.StageName = 'Qualify';    //if 12-31%, stage is in Qualify phase
                              }
                              else OppRecord.StageName = 'Solution';    //if 32-52%, stage is in Solution phase
                        }
                      else OppRecord.StageName = 'Close';    //if 53-88%, stage is in Close phase
                 }
                 else OppRecord.StageName = 'Launch';    //if 89-99%, stage is in Launch phase
             }

        // Here I update the Opportunity's probability:
        if((OppRecord.QQ_Roll_Up__c != OppRecord.Probability) && (OppRecord.QQ_Roll_Up__c != Null)) {
            OppRecord.Probability = OppRecord.QQ_Roll_Up__c;
        }   
    }
}

------------------------------------------
(2) Test Class
@isTest
public class TestUpdateProbTrigger {
    static testMethod void insertNewOpp() {
        
       account acct = new account(Name = 'testing');
       insert acct;
        
        
       Opportunity o = new Opportunity(
               Name = 'testing',
               AccountId = acct.id,
               CloseDate = System.today(),
               StageName = 'Warm Prospect'
       );
       
        insert o;
        
        Opportunity oppty = [SELECT id,
                            Name from Opportunity where Id =: o.Id limit 1];
        
        System.assertEquals(o.Name, 'testing');
            QQ__c q = new QQ__c (
                Name = 'test',
                Opportunity_m__c =o.Id,     //"Opportunity_m" refers to the master-detail field on the QQ
                Opportunity__c = o.Id);        //"Opportunity__c" refers to the opportunity look-up field on the QQ
        
        Insert q;
        
        System.assertEquals('Warm Prospect', o.StageName);
        
        q.D1_x__c = true;
        q.D2_x__c = true;
        q.D3_x__c = true;        
        q.D4_x__c = true;
        q.D5_x__c = true;
        q.D6_x__c = true;
        q.D7_x__c = true;
        q.D8_x__c = true;
        
        update q;

       //the following assignments change the QQ's percentage, and I manipulate these to (ideally) change the opportunity's stage
        q.Q1_x__c = true;
        q.Q2_x__c = true;
        q.Q3_x__c = true;
        q.Q4_x__c = true;
        q.Q5_x__c = true;
        q.Q6_x__c = true;
        q.Q7_x__c = true;
        q.Q8_x__c = true;
        q.Q9_x__c = true;
        q.Q10_x__c = true;
        
        update q;
        System.assertEquals('Solution', o.StageName);    //THIS IS WHERE I GET THE ASSERTION ERROR
        
        q.S1_x__c = true;
        q.S2_x__c = true;
        q.S3_x__c = true;
        q.S4_x__c = true;
        q.S5_x__c = true;
        q.S6_x__c = true;
        q.S7_x__c = true;
        q.S8_x__c = true;
        q.S9_x__c = true;
        
        update q;
        System.assertEquals('Close', o.StageName);
        
        q.S9_x__c = false;

         update q;
        System.assertEquals('Solution', o.StageName);
    }
}

I am not sure why the opportunity in the test class is not changing its stage accordingly. Is it because of the master-detail relationship? I did set it to read/write, though.

Any suggestions would be excellent. Thanks in advance!

-Jeff
Tejpal KumawatTejpal Kumawat
Hello Jeff,

When you Update :
q.Q1_x__c = true;
q.Q2_x__c = true;
q.Q3_x__c = true;
q.Q4_x__c = true;
q.Q5_x__c = true;
q.Q6_x__c = true;
q.Q7_x__c = true;
q.Q8_x__c = true;
q.Q9_x__c = true;
q.Q10_x__c = true;

in your QQ record after this update of QQ Manually.
q.D1_x__c = true;
q.D2_x__c = true;
q.D3_x__c = true;        
q.D4_x__c = true;
q.D5_x__c = true;
q.D6_x__c = true;
q.D7_x__c = true;
q.D8_x__c = true;
Then what is result of your opportunity stage?


 
Jeff GJeff G
@Tejpal

After D1 through D8 are set as true, the Opportunity stage should be 'Qualify'. After Q1 through Q10 are set as true, the Opportunity stage should be 'Solution'.