You need to sign in to do that
Don't have an account?
Matt Cooper 7
De-reference a null object error when attempting to set user variable value to Owner
Hi, I'm putting together a trigger to handle some of my workflow rules (our org is hitting limits) and I keep getting the following error:
"Error:Apex trigger AR_Email_Alerts caused an unexpected exception, contact your administrator: AR_Email_Alerts: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.AR_Email_Alerts: line 62, column 1"
Usually I get this error when I'm trying to set a variable value to a field that is null. However, in this case the value I'm using is OwnerId and on a record that already exists. Can anyone help pinpoint what might be the issue? Thanks!
"Error:Apex trigger AR_Email_Alerts caused an unexpected exception, contact your administrator: AR_Email_Alerts: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.AR_Email_Alerts: line 62, column 1"
Usually I get this error when I'm trying to set a variable value to a field that is null. However, in this case the value I'm using is OwnerId and on a record that already exists. Can anyone help pinpoint what might be the issue? Thanks!
trigger AR_Email_Alerts on Apttus__APTS_Agreement__c (after insert, after update) { String[] toRecipients; String[] ccRecipients; String templateApiName; ID targetObjId; Id whatId; ID orgWideEmailId; Boolean saveAsActivity; Attachment[] attachList; Map<String, Id> typeMap = New Map<String, Id>(); recordtype rt2; Map<string, id> userEmail = new map<string, id>(); ID rtID = Schema.SObjectType.Apttus__APTS_Agreement__c.getRecordTypeInfosByName().get('Appropriations Request').getRecordTypeID(); for (Apttus__APTS_Agreement__c agmt :trigger.new){ if (agmt.recordtypeid == rtID) { //Workflow rule for when AR is Activated - updates fields and sends email to AR Preparer and Requestor if((agmt.Apttus__Status_Category__c == 'Request' && agmt.Apttus__Workflow_Trigger_Viewed_Final__c == true) || (agmt.Apttus_Approval__Approval_Status__c =='Approved' && agmt.AR_Total_Appropriations_Request_Total2__c < 1000) || (agmt.Apttus_Approval__Approval_Status__c =='Approved' && agmt.AR_Capital_Expenditure_Total__c <1000 && agmt.AR_Option_or_New_Lease__c =='Option')){ User user1; User user2; user1.id = agmt.Apttus__Requestor__c; user2.id = agmt.AR_Preparer__c; toRecipients.add(user1.email); toRecipients.add(user2.email); templateApiName = 'AR_Activated'; targetObjId = user1.id; agmt.Apttus__Activated_Date__c = Date.today(); agmt.Apttus__Workflow_Trigger_Viewed_Final__c = false; agmt.Apttus__Status__c = 'Activated'; agmt.Apttus__Status_Category__c = 'In Effect'; } //AR Admin Tasks workflow rule - sends email to Owner/AR Admin else if(agmt.Apttus__Status__c == 'Approved Request' && agmt.AR_Reference_Number__c == '' && agmt.AR_Total_Appropriations_Request_Total2__c >= 1000 && agmt.AR_Option_or_New_Lease__c !='Option'){ User user1; user1.id = agmt.Ownerid; toRecipients.add(user1.email); templateApiName = 'AR_Administrator_Admin_Tasks'; targetObjId = user1.id; } //CPMO Admin pause - sends email to Owner/AR Admin to notify the AR Admin to start the CPMO approvals else if(agmt.Apttus__Status__c == 'Submitted Request' && agmt.Apttus_Approval__Approval_Status__c =='Not Submitted' && agmt.AR_Total_Appropriations_Request_Total2__c >= 20000){ ID oID = agmt.Ownerid; User user1; user1.id = oID; toRecipients.add(user1.email); templateApiName = 'AR_Administrator_CPMO_Admin_Pause'; targetObjId = user1.id; } //Sub-committee Admin pause - sends email to Owner/AR Admin to notify the AR Admin to start the sub-committee approvals else if(agmt.Apttus__Status__c == 'Request Approval' && agmt.Apttus_Approval__Approval_Status__c =='Not Submitted' && agmt.AR_Total_Appropriations_Request_Total2__c >= 1000){ User user1; user1.id = agmt.Ownerid; toRecipients.add(user1.email); templateApiName = 'AR_Administrator_Subcommittee_Admin_Pause'; targetObjId = agmt.Ownerid; } whatId = '01Io00000009t1OEAQ'; orgWideEmailId = '0D2o0000000TNsu'; sendTemplatedEmailClass.sendTemplatedEmail(toRecipients, ccRecipients, templateApiName, targetObjId, whatId, orgWideEmailId); } } }
So you need to do a few things if you're going to fetch the owner's email:
You'll need to requery for the Owner's email address. The reason being is relationship's fields (in this case Owner.Email) aren't available in the trigger.
So before line 13, add the following:
And then lines 50 - 55 could be replaced with:
Looks like you might have some other issues to work out, but that should fix your NullPointerException.
All Answers
You can try initializing it first with something like : User user1 = new User();
So you need to do a few things if you're going to fetch the owner's email:
You'll need to requery for the Owner's email address. The reason being is relationship's fields (in this case Owner.Email) aren't available in the trigger.
So before line 13, add the following:
And then lines 50 - 55 could be replaced with:
Looks like you might have some other issues to work out, but that should fix your NullPointerException.