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
lovetolearnlovetolearn 

Trigger Error

Hi, 

 

I am trying to write a trigger for automatic approval process, but I only want it to run under certain conditions. 

Here is my code:

 

trigger TOApprovalSubmit on Time_Off_Requests__c (after insert) {
    
    Time_Off_Requests__c ToTime;
    
    
            for (Time_Off_Requests__c TOR : trigger.new) {
                Date DateFrom = ToTime.Request_Start_Date__c;
                Date DateTo = ToTime.Request_End_Date__c;
                Integer DaysOff = DateFrom.daysBetween(DateTo);
                
                if(DaysOff > 5){
                Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
                app.setObjectId(TOR.id);
                Approval.ProcessResult result = Approval.process(app);
            
                }
            }
}

 I keep getting a "System.NullPointerException: Attempt to de-reference a null object" error. I think I understand what the error is saying, but I am not sure why I am getting it. The event type is "After Insert" wouldn't that mean that there would be a value for the field that I am trying to reference? 

 

Please help. 

 

Thank you. 

Best Answer chosen by Admin (Salesforce Developers) 
Aar3538Aar3538

Hi there,

 

You are not instantiating a new Time_Off_Request__c and as a result, the variable ToTime is null.  Since you are trying to retrieve ToTime.Request_Start_Date__c, its throwing the error.

 

I am guessing you mean to do the following:

 

Date DateFrom  = TOR.Request_Start_Date__c;

Date DateTo = TOR.Request_End_Date__c;

 

If thats the case I am uncertain what ToTime will be used for.

 

Hope this helps, best of luck.

All Answers

Aar3538Aar3538

Hi there,

 

You are not instantiating a new Time_Off_Request__c and as a result, the variable ToTime is null.  Since you are trying to retrieve ToTime.Request_Start_Date__c, its throwing the error.

 

I am guessing you mean to do the following:

 

Date DateFrom  = TOR.Request_Start_Date__c;

Date DateTo = TOR.Request_End_Date__c;

 

If thats the case I am uncertain what ToTime will be used for.

 

Hope this helps, best of luck.

This was selected as the best answer
Navatar_DbSupNavatar_DbSup

Hi,

 

In trigger use TOR instead of  ToTime in loop.

Try the below modified code:

 

trigger TOApprovalSubmit on Time_Off_Requests__c (after insert) {

   

    Time_Off_Requests__c ToTime;

   

   

            for (Time_Off_Requests__c TOR : trigger.new) {

                Date DateFrom = TOR.Request_Start_Date__c;

                Date DateTo = TOR.Request_End_Date__c;

                Integer DaysOff = DateFrom.daysBetween(DateTo);

               

                if(DaysOff > 5){

                Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();

                app.setObjectId(TOR.id);

                Approval.ProcessResult result = Approval.process(app);

           

                }

            }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

lovetolearnlovetolearn

THANK YOU GUYS!!