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
Adam08Adam08 

System.NullPointerException: Attempt to de-reference a null object

Hi,

 

we have some code that triggers the auto submission of a custom object record  for approval.

we want this to trigger after insert or update where a particular chackbox is ticked which could be on insert or after an update.

 

however we are getting the errors:

- CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY:

- System.NullPointerException: Attempt to de-reference a null object

 

here is the code:

 

trigger MDFSubmitForApproval on MDF__c (after insert, after update) {


for (Integer i = 0; i < trigger.new.size(); i++) 
{
   if(Trigger.old[i].Submitted__c != true && Trigger.new[i].Submitted__c == true)
{
Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
app.setObjectId(Trigger.new[i].id);
}
}
}

 class:

@isTest
private class TestMDFSubmitForApproval {
 
    static testMethod void testApprovalSuccess() {
 
        MDF__c mdf = new MDF__c();
        mdf.Name = 'Test';
        mdf.Account_id__c = '00120000001Sqvv';
        // insert the new MDF
        insert mdf;
        mdf.Submitted__c = true;
        update mdf; 
        
        // ensure that the opp was submitted for approval
        List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :mdf.id];
    System.assertEquals(processInstances.size(),1);
 
    }
 
}

 any ideas?

 

Thanks

ritika@developerforceritika@developerforce

Hi,

 

In an insert trigger, you do not get the Trigger.oldMap, which might be throwing the error.

 

You can try the following :-

 

for (Integer i = 0; i < trigger.new.size(); i++) 
{
   if(Trigger.new[i].Submitted__c == true && (Trigger.isUpdate && Trigger.old[i].Submitted__c != true))
{
Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
app.setObjectId(Trigger.new[i].id);
}
}
}



Hope this helps.

Rahul SharmaRahul Sharma

I guess you may face error in trigger, since you are referring old value in Insert.

So u dont use trigger.old in Insert.

trigger MDFSubmitForApproval on MDF__c (after insert, after update) {


for (Integer i = 0; i < trigger.new.size(); i++) 
{
   if((Trigger.isInsert && Trigger.new[i].Submitted__c == true) || (Trigger.isUpdate && Trigger.old[i].Submitted__c != true && Trigger.new[i].Submitted__c == true))
{
Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
app.setObjectId(Trigger.new[i].id);
}
}
}

 

Adam08Adam08

Thanks Ritika & Rahul :-)

 

that seems to stop that particular error. Although it doesn't seem to like the trigger of the approval.

 

Here is the error at line 18 of the class:

System.AssertException: Assertion Failed: Expected: 0, Actual: 1 TestMDFSubmitForApproval.cls line 18 

Adam08Adam08

Ok, thanks for the responses. The trigger now works, but only on update. and not upon insert of a record with the checkbox set to true.

I can't seem to trigger the approval process if a record is created with the checkbox ticked.

 

here is the code. if anyone could help with this you'd be my hero!

trigger MDFSubmitForApproval on MDF__c (after insert, after update) {


for (Integer i = 0; i < trigger.new.size(); i++) 
{
   if(Trigger.new[i].Submitted__c == true && (Trigger.isUpdate && Trigger.old[i].Submitted__c != true))
{
Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();

app.setObjectId (Trigger.new[i].id);

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

}
}
}

 class:

@isTest
private class TestMDFSubmitForApproval {
 
    static testMethod void testApprovalSuccess() {
 
        MDF__c mdf = new MDF__c();
        mdf.Name = 'Test';
        mdf.Account_id__c = '00120000001Sqvv';
        mdf.Submitted__c = false;
        mdf.Leads_Uploaded__c = true;
        // insert the new MDF
        insert mdf;
        mdf.Submitted__c = true;
        update mdf; 
        
        // ensure that the opp was submitted for approval
        List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :mdf.id];
    System.assertEquals(processInstances.size(),1);
 
    }
 
}

 

 

 

spraetzspraetz

This if statement

 

if(Trigger.new[i].Submitted__c == true && (Trigger.isUpdate && Trigger.old[i].Submitted__c != true))

is always evaluating to false when an insert trigger so your code in the if block isn't firing.

Adam08Adam08

Thanks,

 

so how do i just get the record to save (and not fire the approval) if the criteria is not met?