You need to sign in to do that
Don't have an account?
shrey.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;
}
}
}
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;
}
}
}
// If you want to fatch user base on email id try below code;
Let us know if this will help you
Thanks
Amit Chaudhary
Try this :
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
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
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);
}
}