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
StradaStrada 

Problem with getting updated value from database in Trigger Test Class.

Hi all,

 

I'm wondering if anyone can help me out with my code. I have 2 objects: Opportunity and Change Request (custom object). Both are related via a lookup relationship where a lookup field to the Opportunity object exists on the Change Request custom object. 

 

I am trying to implement functionality whereby when the amount field value on the Opportunity record is changed, then the amount field on the Change Request object should be changed to this value also when the update trigger is fired. I have been able to validate that this has happened in my trigger by printing out values, however, in my test class when I try to pull the data from the database after the update to the opportunity value, the change request value hasn't changed and still retains the original opportunity value.

 

I know I'm probably making a very fundamental mistake but I've been trying to understand what I'm doing wrong but I can't seem to find any answers to my problem.

 

Below is my code, any help or tips to point me in the right direction in finding a solution to my problem would be greatly appreciated.

 

Thanks a million in advance!

 

----------
Trigger
----------

trigger OpportunityUpdateAmountCheck on Opportunity (after update) {
 
 
  list<Opportunity> opps = new list<Opportunity>();
  Decimal crAmount;
  Decimal oppAmount;
 
  for (Opportunity opp : Trigger.new) {
      List<Change_Request__c> changeReqs = [SELECT id, Amount__c, Opportunity__c FROM Change_Request__c WHERE Opportunity__c = :opp.Id];   
      
      
      List<Change_Request__c> crsToUpdate = new List<Change_Request__c>();
      
      for(Change_Request__c thisCR: changeReqs ){
          crAmount = thisCR.Amount__c;
          System.debug('Initial crAmount is: ' + crAmount);
          oppAmount = opp.Amount;
          if(crAmount != oppAmount){
              crAmount = oppAmount;
              crsToUpdate.add(thisCR);
          }
      }
    
      update changeReqs;
      System.debug('Updated crAmount is: ' + crAmount);
  }
}

-------------------
TEST CLASS:
-------------------
    
@isTest
public with sharing class TestOpportunityUpdateAmount {

static testMethod void myUnitTest() {

        //create Account
        Account myAccount = new Account (name='XYZ1 Organization');
        myAccount.BillingCountry = 'Ireland';
        myAccount.Type = 'Prospect';
        myAccount.Status__c = 'Prospect';
        insert myAccount;
                
        Contact mycontact = new Contact (lastName='XYZ Contact',Contact_Status__c = 'Pending',MailingCountry='Ireland');
        mycontact.account = myAccount;
        insert mycontact;
        
        List<Account> savedAccount = [SELECT Status__c, Type FROM Account WHERE name = 'XYZ1 Organization'];
        
        //create Campaign
        Campaign myCampaign = new Campaign(name='XYZ Campaign');
        myCampaign.Type = 'Advertisement';
        insert myCampaign;
        
        Opportunity[] myOpps = new Opportunity[]{
            new Opportunity(name='XYZ Opp', AccountId=savedAccount[0].id, StageName='Open', Campaign=myCampaign, CloseDate = date.today(),
                            amount=90.00,Communication_Type__c = 'Internal Comms'),
            new Opportunity(name='ABC Opp', AccountId=savedAccount[0].id, StageName='Closed-Won', Campaign=myCampaign, CloseDate = date.today(),
                            amount=90.00,Communication_Type__c = 'Internal Comms')
        };
        
        insert myOpps;
        
        Change_Request__c[] myCRs = new Change_Request__c[]{
            new Change_Request__c(Solution__C = myOpps[0].Solution__c, Type__c = myOpps[0].Solution_Category__c,
                Opportunity__c = myOpps[0].Id, Organisation__c = myOpps[0].AccountId, Amount__c = myOpps[0].Amount),
            new Change_Request__c(Solution__C = myOpps[1].Solution__c, Type__c = myOpps[1].Solution_Category__c,
                Opportunity__c = myOpps[1].Id, Organisation__c = myOpps[1].AccountId, Amount__c = myOpps[1].Amount)};
        insert myCRs;
        
        
        myOpps[0].Amount = 150.00;
        update myOpps;
        
        List<Change_Request__c> existingChangeRequest2 = [SELECT id, Amount__c FROM Change_Request__c WHERE Opportunity__c = :myOpps];
        System.assertEquals(myOpps[0].Amount, existingChangeRequest2[0].Amount__c);
        
    }
}

 

Starz26Starz26

Try

@isTest
public with sharing class TestOpportunityUpdateAmount {

static testMethod void myUnitTest() {

        //create Account
        Account myAccount = new Account (name='XYZ1 Organization');
        myAccount.BillingCountry = 'Ireland';
        myAccount.Type = 'Prospect';
        myAccount.Status__c = 'Prospect';
        insert myAccount;
                
        Contact mycontact = new Contact (lastName='XYZ Contact',Contact_Status__c = 'Pending',MailingCountry='Ireland');
        mycontact.accountID = myAccount.ID;
        insert mycontact;
        
               
        //create Campaign
        Campaign myCampaign = new Campaign(name='XYZ Campaign');
        myCampaign.Type = 'Advertisement';
        insert myCampaign;
        
        Opportunity[] myOpps = new Opportunity[]{
            new Opportunity(name='XYZ Opp', AccountId=myAccount.id, StageName='Open', Campaign=myCampaign, CloseDate = date.today(),
                            amount=90.00,Communication_Type__c = 'Internal Comms'),
            new Opportunity(name='ABC Opp', AccountId=myAccount.id, StageName='Closed-Won', Campaign=myCampaign, CloseDate = date.today(),
                            amount=90.00,Communication_Type__c = 'Internal Comms')
        };
        
        insert myOpps;
        
        Change_Request__c[] myCRs = new Change_Request__c[]{
            new Change_Request__c(Solution__C = myOpps[0].Solution__c, Type__c = myOpps[0].Solution_Category__c,
                Opportunity__c = myOpps[0].Id, Organisation__c = myOpps[0].AccountId, Amount__c = myOpps[0].Amount),
            new Change_Request__c(Solution__C = myOpps[1].Solution__c, Type__c = myOpps[1].Solution_Category__c,
                Opportunity__c = myOpps[1].Id, Organisation__c = myOpps[1].AccountId, Amount__c = myOpps[1].Amount)};
        insert myCRs;
        
        
        myOpps[0].Amount = 150.00;
        update myOpps;
        
        List<Change_Request__c> existingChangeRequest2 = [SELECT id, Amount__c FROM Change_Request__c WHERE Opportunity__c = :myOpps[0].ID];
        System.assertEquals(myOpps[0].Amount, existingChangeRequest2[0].Amount__c);
        
    }
}

 

 

A couple of things:

 

1. Once you inserted myAccount, the ID is available so you do not need to do the query.

2. You set the contact.account = myAccount, changed it to accountID = myAccount.id

3. When you queried for the update to the change request, you returned two records. They are not necessairly in the same order asthe myOpps list and the index[0] was most likely related to the myopps[1]. I changed the query to pull only the records related to the myOpps[0] ID and thus they should be correct

 

 

StradaStrada

Hi Starz26,

 

Thanks so much for your reply and suggested amendments to my code. I have implemented your changes as instructed but unfortunately the same thing is happening - the change request amount value doesn't seem to get updated in the database because when I try to run the test it fails for the following reason:

 

|FATAL_ERROR|System.AssertException: Assertion Failed: Expected: 150.00, Actual: 90.00

 

Do you know what else might be causing this to happen?

 

Thanks again for your help so far - I appreciate it!