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
JN22JN22 

Error with Test Class and Trigger for VF Controller

Hi,

 

I created a test class for a custom controller for a VF page off a custom object called Client_Status_Dashboard__c.  As part of my object, I have a trigger called TotalUpdate which updates a number field in the custom object by summing a few other formula fields.  When I run my test class with the trigger de-activated, I get 100% coverage.  However, when I re-activate the trigger, as it is needed for the final product, I get the error below:

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TotalUpdate: execution of BeforeInsert caused by: System.NullPointerException: Argument 1 cannot be null External entry point: []

 

 

My Test Class is as follows:

 

@Istest(SeeAllData=true)
public class TestScorecardController {

public static testMethod void testMyController() {    
        
  Account acct= new Account();
  acct.name='test';
  acct.Type='Employer';
  insert acct;
 
  Client_Status_Scorecard__c cssc = new Client_Status_Scorecard__c();
      cssc.Client_Name__c = acct.id;
      cssc.Client_Licenses__c = 'Portal';
      cssc.Satisfaction_Survery_Score__c = 'A Score of 4-5';
      cssc.Projected_Current_Year_Revenue__c = 'Projected Increase';
      cssc.Engagement_Index_Score__c = 'A Score of 60+';
      cssc.Senior_Level_Relationship__c = 'Positive, Influential';
      cssc.Health_Index_Score__c = 'A Score of 60+';
      cssc.Impact_Index_Score__c = 'A Score of 60+';
     
 
  ApexPages.StandardController sc = new ApexPages.standardController(acct);
  ScorecardController scoreCardCont = new ScorecardController(sc);
  scoreCardCont.score.add(cssc);
   scoreCardCont.save();
 
 }
}

 

And my Trigger is as follows:

 

Trigger TotalUpdate on Client_Status_Scorecard__c (before insert,before update) {

    for (Client_Status_Scorecard__c css1 : Trigger.new) {

    IF((css1.Health_Index_Score__c == null ||

          css1.Health_Index_Score__c == 'Value Index Not Yet Complete or Standalone Client') &&
        (css1.Impact_Index_Score__c == null ||

          css1.Impact_Index_Score__c == 'Value Index Not Yet Complete or Standalone Client'))
     {
        css1.Total_Score_Numerator__c = css1.Satisfaction_Revenue__c + css1.Utilization_Engagement_Score__c + 

                  css1.Relationship_Profile_Score__c;
        css1.Total_Score_Denominator__c = 9.0;
      }

 

    ELSE {
   
        css1.Total_Score_Numerator__c = css1.Satisfaction_Revenue__c + css1.Utilization_Engagement_Score__c +

                  css1.Relationship_Profile_Score__c + css1.Behavior_Change_ROI_Score__c;
        css1.Total_Score_Denominator__c = 12.0;
         
        }
    }
}

 

From what I can gather, the eror is being thrown because the fields that are being summed in the trigger are formula fields that do not have values until after the record is saved so they are null.  However, I'm not sure how to fix that.  Can anyone help?  Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
JN22JN22

Thanks for the replies.  It turns out my problem was in my controller.  I was inadvertantly creating 2 records but only populating 1.  Once I fixed that, my test code worked.

All Answers

Big VBig V

try inserting the record first:

 

Client_Status_Scorecard__c cssc = new Client_Status_Scorecard__c();
      cssc.Client_Name__c = acct.id;
      cssc.Client_Licenses__c = 'Portal';
      cssc.Satisfaction_Survery_Score__c = 'A Score of 4-5';
      cssc.Projected_Current_Year_Revenue__c = 'Projected Increase';
      cssc.Engagement_Index_Score__c = 'A Score of 60+';
      cssc.Senior_Level_Relationship__c = 'Positive, Influential';
      cssc.Health_Index_Score__c = 'A Score of 60+';
      cssc.Impact_Index_Score__c = 'A Score of 60+';

 

insert cssc;

SFDC_VikashSFDC_Vikash

Hi,

 

please check you are not missing any value for a required field on Client_Status_Dashboard__c record 

JN22JN22

Thanks for the replies.  It turns out my problem was in my controller.  I was inadvertantly creating 2 records but only populating 1.  Once I fixed that, my test code worked.

This was selected as the best answer