You need to sign in to do that
Don't have an account?
Shivani 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);
}
}
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);
}
}
All Answers
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.