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 

Test class unable to enter a for loop

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
Naval Sharma4Naval Sharma4
Hi Shivani,

Did you check the field level security for all the profile which you are using in your test methods? Please make sure that fields are visible to the profiles.

Best
kirubakaran viswanathankirubakaran viswanathan
Hi Shivani,

It seems your test class product object's record doesn't have any value for Status__C , that is the reason, it is not moving to next IF logic if(pro.Opportunity_Account__c != null) in your trigger.

add status__c filed value for all product__C object's value definition.
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, status__c = 'Renewed') ;

Hope this will resolve the problem.
Shivani Desai 13Shivani Desai 13
Hi Naval,

Tried updating the profile of the owner in test code to System admin but still didn't work. 

Thanks,
Shivani
Shivani Desai 13Shivani Desai 13
Hi kirubakaran,

Status__c is a formula field which is based on the Contract_Start_Date__c,specific term and auto-renew values and so haven't accessed it in the trigger. 

Thank you,
Shivani
Naval Sharma4Naval Sharma4
Hi Shivani,

Do you know the line number which is the last executed, when you are executing your test method and checking the code coverage in the developer console?
Shivani Desai 13Shivani Desai 13
The number last executed is line number 62 update act; trigger UpdatePackageName on Product__c (after insert,after update) { * List pack = new list();* * Listacc = new list();* * Listact = new list();* * Listat = new list();* * 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;* } All the lines in red are being covered. Shivani Desai | Salesforce Administrator | SiteCompli shivani.desai@sitecompli.com
kirubakaran viswanathankirubakaran viswanathan
Hi Shivani,

Then you need to use the debug log to see the object variables and their values during the trigger.new context and make sure the Formula field (status__C) got the value in it.

Or please share the debug log.

Thanks,
Kiruba
Naval Sharma4Naval Sharma4
I'm sorry Shivani I should have been more clear.  I am interested in the statement which is highlighted as executed. Is the following line blue?
if(pr.Opportunity_Account__r.id == a.id){ 
Shivani Desai 13Shivani Desai 13
Hi Naval, No worries. So I was able to increase the test percentage to 59% by activating a process but my test class started failing. am not sure how exactly to extract that particular statement's value for execution. Should I execute it and look at the logs? I tried to look up some SF documentation but it didn't help. Thanks, Shivani Shivani Desai | Salesforce Administrator | SiteCompli shivani.desai@sitecompli.com
Naval Sharma4Naval Sharma4
Shivani,

You will have to check the logs to find the root cause of the failure. Either you can check the error details from Apex Test Execution or Developer console.

Thanks
kirubakaran viswanathankirubakaran viswanathan
Hi Shivani,

Better enable the debug log and execute the test class, it will show you which line is failing, also you can see the variables values which is going inside the for loop. In that way, you can backtrack.