You need to sign in to do that
Don't have an account?
Help with Trigger Error populating User Lookup field
Hello,
I have the following trigger:
I have a field on the dsfs__DocuSign_Status__c object called dsfs__Sender__c that is a text field. I need it to be a lookup field from the User object though so I can use it for reporting. I created another field called DocuSign_Sender__c that is a lookup to the User. What I am trying to do with this trigger is query the User table and populate the lookup with User's id matching on the String in the dsfs__Sender__c field.
I am getting this error and I'm not sure since I think my logic is pretty much correct:
Apex trigger populateContactfromUser caused an unexpected exception, contact your administrator: populateContactfromUser: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.populateContactfromUser: line 20, column 1
trigger populateDocuSignSenderfromUser on dsfs__DocuSign_Status__c (before insert , before update){ Set<String> setConIds = new Set<String>(); for(dsfs__DocuSign_Status__c obj : trigger.new){ if(obj.dsfs__Sender__c != null) setConIds.add(obj.dsfs__Sender__c); } Map <String, User> mapCon = new Map <String, User>([Select Id from User where Name in: setConIds]); for(dsfs__DocuSign_Status__c obj : trigger.new){ if(obj.dsfs__Sender__c != null){ User c = mapCon.get(obj.dsfs__Sender__c); obj.DocuSign_Sender__c= c.Id; } } }
Please see my comments in Blue.
If this post is helpful throw Kudos. If this post solves your problem please mask it as solution.
Thanks,
Samba
All Answers
What does the sender field stores ? Id or Name ?
If its the name you are doing a query
and this returns Map of record Id and Sobject.
But when you are doing a get here
You are passing the user name, but the map contains Ids as key and hence will return null. You have to create a map of username and Id by iterating over the query.
Please see my comments in Blue.
If this post is helpful throw Kudos. If this post solves your problem please mask it as solution.
Thanks,
Samba
Your error is
"Apex trigger populateContactfromUser caused an unexpected exception, contact your administrator: populateContactfromUser: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.populateContactfromUser: line 20, column 1"
the trigger name is populateContactfromUser
But this trigger name is populateDocuSignSenderfromUser.
It seems that the exception is coming from some other trigger. Please track this and if problem persists please let us know.
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks