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
brozinickrbrozinickr 

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;

          }
       
       }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
sambasamba

Please see my comments in Blue.

 

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 && mapCon.containsKey(obj.dsfs__Sender__c)){
        
            User c = mapCon.get(obj.dsfs__Sender__c);
            
            obj.DocuSign_Sender__c= c.Id;

          }
       
       }
}

 

If this post is helpful throw Kudos. If this post solves your problem please mask it as solution. 

 

Thanks,

Samba

All Answers

Avidev9Avidev9

What does the sender field stores ? Id or Name ?

 

If its the name you are doing a query 

Map <String, User> mapCon = new Map <String, User>([Select Id from User where Name in: setConIds]);

and this returns Map of record Id and Sobject.

 

But when you are doing a get here

User c = mapCon.get(obj.dsfs__Sender__c);

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.

 

 

 

 

sambasamba

Please see my comments in Blue.

 

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 && mapCon.containsKey(obj.dsfs__Sender__c)){
        
            User c = mapCon.get(obj.dsfs__Sender__c);
            
            obj.DocuSign_Sender__c= c.Id;

          }
       
       }
}

 

If this post is helpful throw Kudos. If this post solves your problem please mask it as solution. 

 

Thanks,

Samba

This was selected as the best answer
souvik9086souvik9086

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

user2017user2017
We have an 'Outcome' object with lookup to 'Volunteer hours' object and also to Contact Object. We created a Lookup field in the 'Outcome' object, that should be auto populated based on a field value in Contact or Volunteer hours object and show in newly created records of Outcome Object.  How Is it possible?
user2017user2017
Sorry for posting above question under Answer