+ Start a Discussion
SharathChandraSharathChandra 

Trigger

trigger SendEmailAfterCaseCreated on Case (after insert) {
    for(Case c:trigger.new)
    {
    System.debug(trigger.size);
    System.debug(c.Contact.Email);
    System.debug(c.Status);
    
    }

}

 

 

I'm creating a case for contact which is having email address but i'm not able get email address when i debug

can anyone give the reason

Please help me

SharathChandraSharathChandra

Its giving me null value

kiranmutturukiranmutturu

in triggers you can't get the referenced values you need to query to get the same

SharathChandraSharathChandra

Thanks its working.

Can you provide me reference where it is given?

kirkevonphillykirkevonphilly

Like kiran_mutturu said above, you won't have any of the related values from your trigger.  Look at it this way; it's a trigger on the Case object and you want to pull in information about the related Contact.  When the trigger is running, it would have to pull in all of the related records' data as it doesn't know what you're going to need to access..  Out of the box that would be all the Contact and Account fields, leaving a lot of overhead after every insert.

 

What I'd do is create a separate class that contains all of the code for when a Case meets your criteria, pass a set of IDs for any triggered Contacts (covered for single or bulk), then handle the query in a method within that class and do the rest of your work there.

 

Trigger:

trigger SendEmailAfterCaseCreated oon Case (after insert){
    set<id> tmpSet = new set<id>();
    for (Case c :Trigger.new)
        tmpSet.add(c.ContactID);
   
    SendEmailAfterCaseCreated.handleTrigger(tmpSet);

}

 

 

Class:

SendEmailAfterCaseCreated.cls

public class SendEmailAfterCaseCreated{
    public static void handleTrigger(set<id> ConIDs){
        system.debug('Handed of set to handlTrigger method in SendEmailAfterCaseCreated class' + ConIDs);
        list<Contact> tmpList = [select id, email from Contact where id in:ConIDs];
        if(!tmpList.isEmpty())
            for(Contact c :tmpList){
                system.debug('Contact:  ' + c.id);
                system.debug('Email:  ' + c.email);
                
                ... rest of your work
            }
    }
}