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
Shivani Desai 13Shivani Desai 13 

DML operation failing in trigger

hello all,

I am writing a trigger that sums up the invoice balances from a managed package object Zuora__ZInvoice__c custom field(Zuora__Balance2__c) to a field open_balance__c on the account. I wrote a corresponnding test class for the same and my test classes assertion method is failing. Am not sure why DML operation is failing.
trigger UpdateOpenBalance on Zuora__ZInvoice__c (after insert, after update) {
  
  Set<Id> Acc = new Set<Id>();
  List<account> ade = new List<account>();
  
   for(Zuora__ZInvoice__c zi : trigger.new){
      if(Trigger.isInsert){
          Acc.add(zi.Zuora__Account__c);
      }
         else if (Trigger.isUpdate){
            Acc.add(zi.Zuora__Account__c);
         }
   }
   
   List<Account> AccountsImpacted = [SELECT Id,Open_Balance__c FROM Account WHERE Id IN :Acc];
   List<Zuora__ZInvoice__c> Invoices = [SELECT Id,Zuora__Balance2__c,Zuora__Account__c FROM Zuora__ZInvoice__c WHERE Zuora__Account__c IN:AccountsImpacted];  
   Set<Zuora__ZInvoice__c>InvToAdd = new Set<Zuora__ZInvoice__c>();
   
   for(Zuora__ZInvoice__c inv : Invoices){
      InvToAdd.add(inv);
   }
   
  if(AccountsImpacted.size() > 0){
   for( Account a : AccountsImpacted){
     a.Open_Balance__c = 0.00;
     for( Zuora__ZInvoice__c zd : InvToAdd){
        a.Open_Balance__c = a.Open_Balance__c + zd.Zuora__Balance2__c ;
        ade.add(a);
     }
   }
  }
   
   update ade ;
}

Apex Test Class
@istest(SeeAllData = true)

 public class TestUpdateOpenBalance{
   public static testmethod void UpdateOpenBalance(){
      Account a = new Account();
     // a.RecordType = 'Client';
      a.name = 'Test Account';
      a.Type_of_Client__c = 'Commercial';
      insert a;
      Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];

      User u = new User();
      u.LastName = 'Test Code';
      u.Email = 'test@test.com';
      u.Alias = 'Tcode';
      u.Username = 'ramupdhay@chatopadhay.com';
      u.CommunityNickname = 'test12';
      u.LocaleSidKey = 'en_US';
      u.TimeZoneSidKey = 'GMT';
      u.ProfileID = profileId.id;
      u.LanguageLocaleKey = 'en_US';
      u.EmailEncodingKey = 'UTF-8';
      insert u;
     
     a.CS_Primary__c = u.id;
     update a;
     
     Zuora__CustomerAccount__c ba = new Zuora__CustomerAccount__c();
     ba.name = 'Test Test';
     ba.Zuora__Account__c = a.id;
     ba.Zuora__Credit_Balance__c = 10.00;
     insert ba;
     
     Zuora__ZInvoice__c zi = new Zuora__ZInvoice__c();
     zi.Name = 'Test Invoice';
     zi.Zuora__Account__c = a.id;
     zi.Zuora__Balance2__c = 10.00;
     insert zi;
     
     zi.Zuora__Balance2__c = 20.00;
     update zi;
     
      System.assertEquals(20,a.open_balance__c);
    
   }
 }
 
Best Answer chosen by Shivani Desai 13
SaketJoshiSaketJoshi
When you perform the DML operations and write asserts in the test classes, the new values are not copied over to the existing instances of the objects. You need to query to get the newly calculated values. Add a query before your assert and it will get the new values.
Account acc = [SELECT Id, Open_Balance__c FROM Account WHERE Id =: a.Id LIMIT 1];
System.assertEquals(20, acc.Open_Balance__c);

 

All Answers

SaketJoshiSaketJoshi
When you perform the DML operations and write asserts in the test classes, the new values are not copied over to the existing instances of the objects. You need to query to get the newly calculated values. Add a query before your assert and it will get the new values.
Account acc = [SELECT Id, Open_Balance__c FROM Account WHERE Id =: a.Id LIMIT 1];
System.assertEquals(20, acc.Open_Balance__c);

 
This was selected as the best answer
Shivani Desai 13Shivani Desai 13
Thank you Saket! I updated the test class and it worked. My trigger and test class work when I try to add invoice manually or through a test class but when the app(Zuora) integrates with Salesforce and adds invoices to Salesforce it ends up giving wrong invoice balance results. 
for( Account a : AccountsImpacted){
     a.Open_Balance__c = 0.00;

The above line of code in the first for loop should ensure that the account open balance value is set to zero before adding the invoice balances right? Am so confused. Appreciate all your help.