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
jonathanbernddf20131.387825590468351E12jonathanbernddf20131.387825590468351E12 

Need help - Not able to update value in Test class record

Hi folks
As a newbie to SF, can someone explain to me why nothing I do actually updates my record AND activates my trigger?
I have a trigger on Opportunity to a custom object. It creates a record based on the stage value. Trigger and Class work in sandbox.
I'm trying to get test coverage and I'm running into a roadblock.

I have managed to update a StageName value in the Test class but it didn't call the trigger. (which is supposed to update a previous custom object record and create a new one with the new StageName).
I tried putting in Test.startTest(); and Test.stopTest(); but it didn't make a difference. 
I tried creating bulk records, but only succeeded in creating multiple initial inserts, but did not manage to call the update part of the trigger.
I tried a single opportunity test record, and did update the value in StageName but did not call the trigger.
I'm still trying with a single record and have the test code below.  Please can someone explain to me what I clearly don't understand? Thanks
@isTest
public class TestOppStageHistory {

    static testmethod void createInsOppData() {
        
        Test.startTest();
        
        //Create an opportunity
        Opportunity insOpp = new Opportunity();
        insOpp.Name = 'TestUpdate';
        insOpp.StageName = 'Qualification/Prospecting';
        insOpp.Probability = 1;
        insOpp.CloseDate = date.TODAY() + 10;
        insOpp.Type = 'New Business';
        insert insOpp;

      
        //Test to see if opportunity stage history record was created
        List<Opportunity> insTestOpps = [Select Id, StageName FROM Opportunity WHERE Id = :insOpp.Id];
        String stval1 = insTestOpps[0].Id;   
        System.debug('Value of Id in Opportunity in record is ' + stval1);
            
        Opportunity_Stage_History__c newOppsh = [Select Id, Opportunity__c, Stage__c, Start_Date__c, Completion_Date__c 
                                                 FROM Opportunity_Stage_History__c WHERE Opportunity__c = :insOpp.Id];

        System.assertEquals(newOppsh.Stage__c, insOpp.StageName);
        System.assertEquals(newOppsh.Start_Date__c, date.TODAY());
        System.assertEquals(newOppsh.Completion_Date__c, NULL);
        System.assertEquals(newOppsh.Opportunity__c, insOpp.Id);
            
        //Do we have the right values?
        List<Opportunity_Stage_History__c> insertedShRecs= [SELECT Id, Stage__c, Start_Date__c, Completion_Date__c, Opportunity__c
                                                            FROM Opportunity_Stage_History__c WHERE Opportunity__c = : insOpp.Id ];
        System.debug('The number of recs in the list is ' + insertedShRecs.size());
        String stval2 = insertedShRecs[0].Stage__c;   
        System.debug('Value of Stage__c in record is ' + stval2);
        

        //update StageName on opportunity

        Opportunity updOpp = [SELECT Id, StageName, Probability FROM Opportunity WHERE Id= :insOpp.Id];
        String stval3 = updOpp.Id;
        System.debug('Value of Id in updOpp record is ' + stval3); 
        String stval4 = updOpp.StageName;
        System.debug('Value of StageName in updOpp record is '+stval4);
        
        updOpp.StageName = 'Needs Analysis';
        updOpp.Probability = 25;
        //update.updOpp(); - can't do this. Do I need to map something? Do I need to break to two methods - how to use same record?
        
        Test.stopTest();
        
    }
    
}

Best Answer chosen by jonathanbernddf20131.387825590468351E12
jonathanbernddf20131.387825590468351E12jonathanbernddf20131.387825590468351E12
I was really dumb. My execution context check was stopping the test.

All Answers

Prem_PalPrem_Pal
I see that you have commented the line which updates the Opportunity.

update.updOpp();


Could you uncomment it and use it as given below, because above syntax is wrong,

update updOpp;


This should invoke your update trigger.


Also you need not query the object to update it you could simply update the object that you have inserted

insOpp.StageName = 'Needs Analysis';

update insOpp;


If this does not work check if your trigger has update context declared. e.g.

trigger oppTestTrggr on Opportunity (before insert, before update,after update) {
}


Hope this helps!

Thanks,
Prem
jonathanbernddf20131.387825590468351E12jonathanbernddf20131.387825590468351E12
Thanks for getting back to me. Can't believe I did that with update!  Anyway, my basic problem still remains. I can't cause the trigger to fire for the update. Any help would be great. I've been going in circles in this for a while. I'm including the class and trigger and revised test class. (System.debug should show two opportunity stage history records after update.)
public with sharing class OppStageHistory {

  //Initialize Static Variable
  public Static Boolean firstcall=True;    
  
  //Method to insert new Opportunity Stage History record with brand new Opportunity
  public static void updOppStageHistory(List<Opportunity> oldOpp, List<Opportunity> newOpp){

  if(!firstcall && newOpp[0].Name != 'Trigger') {
  //System.debug('Exiting method to update OPportunity Stage History because of !firstcall');
    return;}
    else {firstcall=false;}
    
   //Declare a set of the Ids in this update
   Set<Id> oppStageIds = new Set<Id>();
   for (Opportunity oppO : oldOpp) {
   //Grab the Ids from the opp List
   oppStageIds.add(oppO.Id);
   }

   //Declare a list of Opportunity Stage History
   List <Opportunity_Stage_History__c> prevOpshList = new List <Opportunity_Stage_History__c> ();
   
   //Fill the list with records that match the Id from the trigger and has a Null Completion Date
   prevOpshList = [SELECT Id, Name, Stage__c, Start_Date__c, Completion_Date__c FROM Opportunity_Stage_History__c 
                   WHERE Opportunity__c IN :oppStageIds AND Completion_Date__c = NULL]; 
                   
   //For each record in prevOpshList, set the Completion date
   for(Opportunity_Stage_History__c prevOpsh : prevOpshList) {
       prevOpsh.Completion_Date__c = date.TODAY();
    }
       
       //Update the rows in the list
       try {
           update prevOpshList;
           }
           catch (DmlException e) {
                      // Process exception here
           }
    //Declare a new List of Opportunity Stage History
    List <Opportunity_Stage_History__c> opsh = new List <Opportunity_Stage_History__c> ();
    for(Opportunity oppN: newOpp) {
              integer i = 0;   //Keeps oldOpp in sync with oppN which is newOpp 
              System.debug('oldOpp Stage is ' + oldOpp[i].StageName + '-- oppN.Stage is  ' + oppN.StageName);
              if(oppN.StageName <> oldOpp[i].StageName && oldOpp[i].StageName <> NULL) {
                   Opportunity_Stage_History__c newOpsh = new Opportunity_Stage_History__c();
                   newOpsh.Stage__c = oppN.StageName;
                   newOpsh.Start_Date__c = date.TODAY();
                   newOpsh.Opportunity__c = oppN.Id;
          
                   //System.debug('adding ' + String.valueOf(oppN.Id) + ' to List newOpsh');
                   opsh.add(newOpsh);
                 } else { //This handles the exeption when the previous Stage was blank but the Opportunity has been created with no stage
                     
                       if(oppN.StageName <> oldOpp[i].StageName) {
                           Opportunity_Stage_History__c newOpsh = new Opportunity_Stage_History__c();
                           newOpsh.Stage__c = oppN.StageName;
                           newOpsh.Start_Date__c = date.TODAY();
                           newOpsh.Opportunity__c = oppN.Id;
          
                           opsh.add(newOpsh);
                           i = i + 1;
                         }

                       } // end of else
               } // end of for loop oppN 
               //Insert the new rows
               insert opsh; 
  } //end of updOppStageHistory Method                     

  public static void createOppStageHistory(List<Opportunity> newOpp)
  {
   if(!firstcall) {
     System.debug('Exiting updOppStageHistory because of !firstcall');
     return;}
     else {firstcall=false;}
   
   List <Opportunity_Stage_History__c> opsh = new List <Opportunity_Stage_History__c> ();
   for(Opportunity oppN: newOpp) {
          Opportunity_Stage_History__c newOpsh = new Opportunity_Stage_History__c();
      
         //Set Values for New Opportunity
             if(oppN.StageName <> NULL && oppN.StageName <> '--None--')
             {
                newOpsh.Stage__c = oppN.StageName;
                newOpsh.Start_Date__c = date.TODAY();
                newOpsh.Opportunity__c = oppN.Id;
       
                opsh.add(newOpsh);
             }
          }
    insert opsh;
  }
 
}
trigger OppStageHistory on Opportunity (after insert, after update) {

    if(Trigger.isInsert) {
        OppStageHistory.createOppStageHistory(Trigger.new);
    }
    if(Trigger.isUpdate) {
        for (Opportunity newOpp: Trigger.new)  {
            Integer i = 0;
            if(newOpp.StageName != Trigger.old[i].StageName) {
                OppStageHistory.updOppStageHistory(Trigger.old, Trigger.new);
            }
            i++;
        }
    }
}
@isTest
public class TestOppStageHistory {

    static testmethod void createInsOppData() {
       
        Test.startTest();
       
        //Create an opportunity
        Opportunity insOpp = new Opportunity();
        insOpp.Name = 'Trigger';
        insOpp.StageName = 'Qualification/Prospecting';
        insOpp.Probability = 1;
        insOpp.CloseDate = date.TODAY() + 10;
        insOpp.Type = 'New Business';
        insert insOpp;

     
        //Test to see if opportunity stage history record was created
        List<Opportunity> insTestOpps = [Select Id, StageName FROM Opportunity WHERE Id = :insOpp.Id];
        String stval1 = insTestOpps[0].Id;  
        System.debug('Value of Id in Opportunity in record is ' + stval1);
           
        Opportunity_Stage_History__c newOppsh = [Select Id, Opportunity__c, Stage__c, Start_Date__c, Completion_Date__c
                                                 FROM Opportunity_Stage_History__c WHERE Opportunity__c = :insOpp.Id];

        System.assertEquals(newOppsh.Stage__c, insOpp.StageName);
        System.assertEquals(newOppsh.Start_Date__c, date.TODAY());
        System.assertEquals(newOppsh.Completion_Date__c, NULL);
        System.assertEquals(newOppsh.Opportunity__c, insOpp.Id);
           
        //Do we have the right values?
        List<Opportunity_Stage_History__c> insertedShRecs= [SELECT Id, Stage__c, Start_Date__c, Completion_Date__c, Opportunity__c
                                                            FROM Opportunity_Stage_History__c WHERE Opportunity__c = : insOpp.Id ];
        System.debug('The number of recs in the list is ' + insertedShRecs.size());
        String stval2 = insertedShRecs[0].Stage__c;  
        System.debug('Value of Stage__c in record is ' + stval2);
       

        //update StageName on opportunity

        Opportunity updOpp = [SELECT Id, StageName, Probability FROM Opportunity WHERE Id= :insOpp.Id];
        String stval3 = updOpp.Id;
        System.debug('Value of Id in updOpp record is ' + stval3);
        String stval4 = updOpp.StageName;
        System.debug('Value of StageName in updOpp record is '+stval4);
       
        updOpp.StageName = 'Needs Analysis';
        updOpp.Probability = 25;
        update updOpp;
       
        String stval5 = updOpp.StageName;
        System.debug('Value of StageName in updOpp record is '+stval5);
       
        //Did trigger work on new stage history and update old?

        List<Opportunity_Stage_History__c> updatedShRecs= [SELECT Id, Stage__c, Start_Date__c, Completion_Date__c, Opportunity__c
                                                            FROM Opportunity_Stage_History__c WHERE Opportunity__c = : updOpp.Id];
        System.debug('The number of recs in the list is ' + insertedShRecs.size());
        String stval6 = updatedShRecs[0].Stage__c;
        System.debug ('Value of Stage__c in updatedShRecs record is '+stval6);
        //String stval7 = updatedShRecs[1].Stage__c;
        //System.debug ('Value of Stage__c in updatedShRecs record is '+stval7);
       
        Test.stopTest();
       
    }
   
}





jonathanbernddf20131.387825590468351E12jonathanbernddf20131.387825590468351E12
Actually partially solved myself. Have an issue in the class (not test class).
Prem_PalPrem_Pal
What part exaclty is not getting invoked. Could you try putting a debug statement in the trigger itself?


Thanks,
Prem
jonathanbernddf20131.387825590468351E12jonathanbernddf20131.387825590468351E12
I was really dumb. My execution context check was stopping the test.
This was selected as the best answer