• Shivani Desai 13
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 5
    Replies
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);
    
   }
 }
 
Hi All.
My business logic needs me to update the value of the highest package name associated to an account. This package name is listed on my custom product object. I wrote a trigger on product that checks for the product value and updates the account. But the test class is not entering the product loop no matter what I do and neither is my test percentage increasing. 

Trigger: 
trigger UpdatePackageName on Product__c (after insert,after update) {
  List<Product__c> pack = new list<product__c>();
  List<Account>acc = new list<account>();
  List<Account>act = new list<account>();
  List<Id>at = new list<Id>();
  
  for( product__c pro: trigger.new){
     if (pro.Product_Family__c == 'SiteCompli NY'){ 
       if( pro.Product__c != 'FDNY Compliance'){
         if((pro.Status__c=='Active'||pro.Status__c=='Renewed')){
           if(pro.Opportunity_Account__c!= null){
             
              
                 at.add(pro.Opportunity_Account__c);
                 pack.add(pro);
             
            }
          }
        }
     }
  }

    acc= [SELECT Id, Package_Name__c FROM Account WHERE ID IN :at];
     for(Account a: acc){
     for( product__c pr: pack){
            
              if((pr.status__c=='Active'||pr.status__c=='Renewed')){
               if(pr.Product_Family__c == 'SiteCompli NY'){
                 if(pr.Product__c != 'FDNY Compliance'){
               
                   
                     if(pr.Opportunity_Account__r.id == a.id){
                       if(a.package_name__c != pr.Product__c){
                        if(pr.product__c =='Platinum'&& a.package_name__c!='Platinum'){
                         a.package_name__c = 'Platinum';
                         act.add(a);
                        }
                        else if(pr.product__c == 'Gold' &&  a.package_name__c!='Platinum'){
                        a.package_name__c = 'Gold';
                        act.add(a);
                        }
                        else if(pr.product__c == 'Silver'&& (a.package_name__c!='Platinum'||a.package_name__c!='Gold')){
                        a.package_name__c = 'Silver';
                        act.add(a);
                        }
                        
                        else if(a.package_name__c == 'Grandfather - SC'||a.package_name__c == 'Grandfather - ENY'){
                         a.package_name__c = pr.product__c;
                        act.add(a);
                        }
                        
                     
                   }
                 }
               }
             }
            }
          }
         
        }
    
    update act;
}

Test Class:


@istest(SeeAllData = true)
 public class TestUpdatePackageNameanything{
   public static testmethod void testUpdatePackageName (){
     Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
      Profile Prof = [SELECT Id FROM Profile WHERE Name = 'System Administrator' 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;
     
       User ut = new User();
      ut.LastName = 'Worm';
      ut.Email = 'test@gmail.com';
      ut.Alias = 'Tworm';
      ut.Username = 'test@worm.com';
      ut.CommunityNickname = 'testworm12';
      ut.LocaleSidKey = 'en_US';
      ut.TimeZoneSidKey = 'GMT';
      ut.ProfileID = profileId.id;
      ut.LanguageLocaleKey = 'en_US';
      ut.EmailEncodingKey = 'UTF-8';
      insert ut;
      
      Account a = new account(name='Test Account',Sales_Associate__c=ut.id,Sales_Owner__c=u.id,package_name__c='');
      insert a;
      Id abc = a.id;

      
      Contact c = new contact(firstname = 'Test',lastname = 'McAllister',accountId=a.id);
      insert c;
      
      Opportunity O = new Opportunity(name = 'Test Opportunity', CloseDate = date.newInstance(2016,7,12),StageName='Needs Analysis');
      O.AccountId = a.id;
      O.primary_contact__c= c.id;
      insert O;
      
       Product__c p = new product__c(Product__c = 'Gold',Product_Family__c = 'Sitecompli NY',Billing_Frequency__c = 'Annually',Term_Length_Months__c = 12,Total_Commitment_Amount__c=400.00,Commitment_Terms__c='Specific', Opportunity__c = o.id, Contract_Start_Date__c = date.newInstance(2017,2,1),First_Bill_Date__c = date.newInstance(2017,2,1),auto_renew__c = true) ;
        
       insert p;
       p.Opportunity_Account__c = abc;
       update p;
       
      // p.Opportunity_Primary_Contact__c = c.id;
      
       Product__c pt = new product__c(Product__c = 'Silver',Product_Family__c = 'Sitecompli NY',Billing_Frequency__c = 'Annually',Term_Length_Months__c = 12,Total_Commitment_Amount__c=400.00,Commitment_Terms__c='Specific', Opportunity__c = o.id, Contract_Start_Date__c = date.newInstance(2017,2,1),First_Bill_Date__c = date.newInstance(2017,2,1)) ;
      
       insert pt;
        p.Opportunity_Account__c = abc;
      update pt;
       
      
      O.StageName = 'Closed/Won';
      update o;
      
      p.product__c = 'Silver';
       update p;
       update pt;
      
    
      
      
      
      
      
    }
    
    public static testmethod void testUpdatePackageName1 (){
    Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
      Profile Prof = [SELECT Id FROM Profile WHERE Name = 'System Administrator' 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;
     
       User ut = new User();
      ut.LastName = 'Worm';
      ut.Email = 'test@gmail.com';
      ut.Alias = 'Tworm';
      ut.Username = 'test@worm.com';
      ut.CommunityNickname = 'testworm12';
      ut.LocaleSidKey = 'en_US';
      ut.TimeZoneSidKey = 'GMT';
      ut.ProfileID = profileId.id;
      ut.LanguageLocaleKey = 'en_US';
      ut.EmailEncodingKey = 'UTF-8';
      insert ut;
  
    Account ab = new account(name='Test Account',Sales_Associate__c=ut.id,Sales_Owner__c=u.id,package_name__c='Platinum');
      insert ab;
      Id abd = ab.id;
      
      Contact c = new contact(firstname = 'Test',lastname = 'McAllister',accountId=ab.id);
      insert c;
      
    Opportunity Opp = new Opportunity(name = 'Test Opportunity', CloseDate = date.newInstance(2016,7,12),StageName='Closed/Won');
      Opp.AccountId = ab.id;
      Opp.primary_contact__c= c.id;
      insert Opp;
      
      Product__c pr = new product__c(Product__c = 'Platinum',Canceled_Date__c=null,Product_Family__c = 'Sitecompli NY',Billing_Frequency__c = 'Annually',Term_Length_Months__c = 12,Total_Commitment_Amount__c=400.00,Commitment_Terms__c='Specific', Opportunity__c = opp.id,First_Bill_Date__c = date.newInstance(2017,2,1)) ;
       insert pr;
       pr.Opportunity_Account__c = abd;
       update pr;
       Product__c prt = new product__c(Product__c = 'FDNY Compliance',Product_Family__c = 'Sitecompli NY',Billing_Frequency__c = 'Annually',Term_Length_Months__c = 12,Total_Commitment_Amount__c=400.00,Commitment_Terms__c='Specific', Opportunity__c = opp.id,First_Bill_Date__c = date.newInstance(2017,2,1)) ;
       insert prt;
       
      update prt;
    }
   }

The test coverage is stopping at if(pro.Opportunity_Account__c != null) which is a lookup field that associates the account with product. Have been trying to get this to work for 2-3 days now and nothing works. Please HELP!

Thanks,
Shivani
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);
    
   }
 }
 
Hi All.
My business logic needs me to update the value of the highest package name associated to an account. This package name is listed on my custom product object. I wrote a trigger on product that checks for the product value and updates the account. But the test class is not entering the product loop no matter what I do and neither is my test percentage increasing. 

Trigger: 
trigger UpdatePackageName on Product__c (after insert,after update) {
  List<Product__c> pack = new list<product__c>();
  List<Account>acc = new list<account>();
  List<Account>act = new list<account>();
  List<Id>at = new list<Id>();
  
  for( product__c pro: trigger.new){
     if (pro.Product_Family__c == 'SiteCompli NY'){ 
       if( pro.Product__c != 'FDNY Compliance'){
         if((pro.Status__c=='Active'||pro.Status__c=='Renewed')){
           if(pro.Opportunity_Account__c!= null){
             
              
                 at.add(pro.Opportunity_Account__c);
                 pack.add(pro);
             
            }
          }
        }
     }
  }

    acc= [SELECT Id, Package_Name__c FROM Account WHERE ID IN :at];
     for(Account a: acc){
     for( product__c pr: pack){
            
              if((pr.status__c=='Active'||pr.status__c=='Renewed')){
               if(pr.Product_Family__c == 'SiteCompli NY'){
                 if(pr.Product__c != 'FDNY Compliance'){
               
                   
                     if(pr.Opportunity_Account__r.id == a.id){
                       if(a.package_name__c != pr.Product__c){
                        if(pr.product__c =='Platinum'&& a.package_name__c!='Platinum'){
                         a.package_name__c = 'Platinum';
                         act.add(a);
                        }
                        else if(pr.product__c == 'Gold' &&  a.package_name__c!='Platinum'){
                        a.package_name__c = 'Gold';
                        act.add(a);
                        }
                        else if(pr.product__c == 'Silver'&& (a.package_name__c!='Platinum'||a.package_name__c!='Gold')){
                        a.package_name__c = 'Silver';
                        act.add(a);
                        }
                        
                        else if(a.package_name__c == 'Grandfather - SC'||a.package_name__c == 'Grandfather - ENY'){
                         a.package_name__c = pr.product__c;
                        act.add(a);
                        }
                        
                     
                   }
                 }
               }
             }
            }
          }
         
        }
    
    update act;
}

Test Class:


@istest(SeeAllData = true)
 public class TestUpdatePackageNameanything{
   public static testmethod void testUpdatePackageName (){
     Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
      Profile Prof = [SELECT Id FROM Profile WHERE Name = 'System Administrator' 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;
     
       User ut = new User();
      ut.LastName = 'Worm';
      ut.Email = 'test@gmail.com';
      ut.Alias = 'Tworm';
      ut.Username = 'test@worm.com';
      ut.CommunityNickname = 'testworm12';
      ut.LocaleSidKey = 'en_US';
      ut.TimeZoneSidKey = 'GMT';
      ut.ProfileID = profileId.id;
      ut.LanguageLocaleKey = 'en_US';
      ut.EmailEncodingKey = 'UTF-8';
      insert ut;
      
      Account a = new account(name='Test Account',Sales_Associate__c=ut.id,Sales_Owner__c=u.id,package_name__c='');
      insert a;
      Id abc = a.id;

      
      Contact c = new contact(firstname = 'Test',lastname = 'McAllister',accountId=a.id);
      insert c;
      
      Opportunity O = new Opportunity(name = 'Test Opportunity', CloseDate = date.newInstance(2016,7,12),StageName='Needs Analysis');
      O.AccountId = a.id;
      O.primary_contact__c= c.id;
      insert O;
      
       Product__c p = new product__c(Product__c = 'Gold',Product_Family__c = 'Sitecompli NY',Billing_Frequency__c = 'Annually',Term_Length_Months__c = 12,Total_Commitment_Amount__c=400.00,Commitment_Terms__c='Specific', Opportunity__c = o.id, Contract_Start_Date__c = date.newInstance(2017,2,1),First_Bill_Date__c = date.newInstance(2017,2,1),auto_renew__c = true) ;
        
       insert p;
       p.Opportunity_Account__c = abc;
       update p;
       
      // p.Opportunity_Primary_Contact__c = c.id;
      
       Product__c pt = new product__c(Product__c = 'Silver',Product_Family__c = 'Sitecompli NY',Billing_Frequency__c = 'Annually',Term_Length_Months__c = 12,Total_Commitment_Amount__c=400.00,Commitment_Terms__c='Specific', Opportunity__c = o.id, Contract_Start_Date__c = date.newInstance(2017,2,1),First_Bill_Date__c = date.newInstance(2017,2,1)) ;
      
       insert pt;
        p.Opportunity_Account__c = abc;
      update pt;
       
      
      O.StageName = 'Closed/Won';
      update o;
      
      p.product__c = 'Silver';
       update p;
       update pt;
      
    
      
      
      
      
      
    }
    
    public static testmethod void testUpdatePackageName1 (){
    Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
      Profile Prof = [SELECT Id FROM Profile WHERE Name = 'System Administrator' 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;
     
       User ut = new User();
      ut.LastName = 'Worm';
      ut.Email = 'test@gmail.com';
      ut.Alias = 'Tworm';
      ut.Username = 'test@worm.com';
      ut.CommunityNickname = 'testworm12';
      ut.LocaleSidKey = 'en_US';
      ut.TimeZoneSidKey = 'GMT';
      ut.ProfileID = profileId.id;
      ut.LanguageLocaleKey = 'en_US';
      ut.EmailEncodingKey = 'UTF-8';
      insert ut;
  
    Account ab = new account(name='Test Account',Sales_Associate__c=ut.id,Sales_Owner__c=u.id,package_name__c='Platinum');
      insert ab;
      Id abd = ab.id;
      
      Contact c = new contact(firstname = 'Test',lastname = 'McAllister',accountId=ab.id);
      insert c;
      
    Opportunity Opp = new Opportunity(name = 'Test Opportunity', CloseDate = date.newInstance(2016,7,12),StageName='Closed/Won');
      Opp.AccountId = ab.id;
      Opp.primary_contact__c= c.id;
      insert Opp;
      
      Product__c pr = new product__c(Product__c = 'Platinum',Canceled_Date__c=null,Product_Family__c = 'Sitecompli NY',Billing_Frequency__c = 'Annually',Term_Length_Months__c = 12,Total_Commitment_Amount__c=400.00,Commitment_Terms__c='Specific', Opportunity__c = opp.id,First_Bill_Date__c = date.newInstance(2017,2,1)) ;
       insert pr;
       pr.Opportunity_Account__c = abd;
       update pr;
       Product__c prt = new product__c(Product__c = 'FDNY Compliance',Product_Family__c = 'Sitecompli NY',Billing_Frequency__c = 'Annually',Term_Length_Months__c = 12,Total_Commitment_Amount__c=400.00,Commitment_Terms__c='Specific', Opportunity__c = opp.id,First_Bill_Date__c = date.newInstance(2017,2,1)) ;
       insert prt;
       
      update prt;
    }
   }

The test coverage is stopping at if(pro.Opportunity_Account__c != null) which is a lookup field that associates the account with product. Have been trying to get this to work for 2-3 days now and nothing works. Please HELP!

Thanks,
Shivani