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
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Null Pointer Exception Error- Apex Case Trigger(After Insert)- Please help!!!

Hi Everyone,
        I have written an apex trigger that fetches the contact email from a caser, matches that email with users in salesforce and assigns the matched user the ownership of that case. Please find the code given below:

The code works well, the map is coming out to be as expected in debug statement . But I am getting a null pointer in the bold lines. Can anyone please help me?

trigger AssignFDMCaseOwner on Case (after insert) {
   //create an empty list to hold the contact email IDs
   List<String> contactmailIds = new List<String>();
    for (Case c :Trigger.new) {    
        if ( c.SuppliedEmail!=null) {
            //add the contact ids to the list
            contactmailIds.add(c.SuppliedEmail);         
        }
    }

    //Query for the contacts and add them to a map
   

    Map<String,User> UserMap = new Map<String,User>([SELECT Email,Id  FROM User WHERE FirstName ='Shrey' AND Email IN :contactmailIds Limit 1]);
    system.debug('aaaaaaaaaaaaaaaaaaaaaaaaaaaa'+ UserMap);
    
    for(Case c :Trigger.new) {
       if(c.SuppliedEmail!=Null && UserMap.get(c.SuppliedEmail).Id!=null){
           c.OwnerId = UserMap.get(c.SuppliedEmail).Id;

           update c;
       }
              
    }
}
Amit Chaudhary 8Amit Chaudhary 8
Please use containsKey to check value in map
trigger AssignFDMCaseOwner on Case (before insert) 
{
	List<String> contactmailIds = new List<String>();
    for (Case c :Trigger.new) 
	{
        if ( c.SuppliedEmail!=null ) 
		{
            contactmailIds.add(c.SuppliedEmail);         
        }
    }

    Map<String,User> UserMap = new Map<String,User>([SELECT Email,Id  FROM User WHERE FirstName ='Shrey' AND Email IN :contactmailIds Limit 1]);
	
	
    for(Case c :Trigger.new) 
	{
        if( c.SuppliedEmail!=Null && UserMap.containsKey(c.SuppliedEmail) )
	    {
           c.OwnerId = UserMap.get(c.SuppliedEmail).Id;
		   
           //update c; --- use before insert no need to update
        }
    }
}

    // If you want to fatch user base on email id try below code;

 
trigger AssignFDMCaseOwner on Case (before insert) 
{
	List<String> contactmailIds = new List<String>();
    for (Case c :Trigger.new) 
	{
        if ( c.SuppliedEmail!=null ) 
		{
            contactmailIds.add(c.SuppliedEmail);         
        }
    }

    Map<String,User> UserMap = new Map<String,User>([SELECT Email,Id  FROM User WHERE FirstName ='Shrey' AND Email IN :contactmailIds Limit 1]);

	// If you want to fatch user base on email id try below code;
	Map<String,User> UserMap = new Map<String,User>();
	List<User> lstUser = [SELECT Email,Id  FROM User WHERE FirstName ='Shrey' AND Email IN :contactmailIds Limit 1] ;
	
	for(user usr: lstUser)
	{
		UserMap.put(usr.email ,usr );
	}
	
    for(Case c :Trigger.new) 
	{
        if( c.SuppliedEmail!=Null && UserMap.containsKey(c.SuppliedEmail) )
	    {
           c.OwnerId = UserMap.get(c.SuppliedEmail).Id;
		   
        }
    }
}

Let us know if this will help you

Thanks
Amit Chaudhary

 
Ajay K DubediAjay K Dubedi
Hi,
Try this :
trigger AssignFDMCaseOwner on Case (before insert) {
   //create an empty list to hold the contact email IDs
   List<String> contactmailIds = new List<String>();
    for (Case c :Trigger.new) {    
        if ( c.SuppliedEmail!=null) {
            //add the contact ids to the list
            contactmailIds.add(c.SuppliedEmail);         
        }
    }

    //Query for the contacts and add them to a map
   

    Map<String,User> UserMap = new Map<String,User>([SELECT Email,Id  FROM User WHERE FirstName ='Shrey' AND Email IN :contactmailIds Limit 1]);
    system.debug('aaaaaaaaaaaaaaaaaaaaaaaaaaaa'+ UserMap);
    
    for(Case c :Trigger.new) {
       if(c.SuppliedEmail!=Null && UserMap.get(c.SuppliedEmail).Id!=null){
           c.OwnerId = UserMap.get(c.SuppliedEmail).Id;
       }
              
    }
}

The reason of error is "after Insert". Once the record is Inserted, trigger.new becomes empty.

Please view this link for more :

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables_considerations.htm
Regards,
Ajay

Salesforce####Salesforce####
hello all  , 

i am not understanding how to fill the details into the supplyemail in case ... i tried alot but i couldnt succedd how to do it on case layout 
Salesforce####Salesforce####
// i am fetching email id from contact directly as i dnt  see supply email id : and i am getting null value s: can you please kindly correct my code 

in before insert , null value will appear for contact wrt to cas please refer the help link below 
https://help.salesforce.com/HTViewSolution?id=000221321&language=en_US

my code : 
trigger AssignFDMCaseOwner on Case (before insert)
{
//create an empty list to hold the contact email IDs
List<String> contactmailIds = new List<String>();

for (Case c :Trigger.new)
{
if ( c.contact != null)    // in debug i can see null value cmng here . 
{

contactmailIds.add(c.ContactEmail);
}
}