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
Scott CarlsonScott Carlson 

How to reference Case owner in Apex on New Case Creation?

I am trying some things out on my road to learning Apex.

Here is my current challenge: I want to send details on a new case via email to a specified email address, only when a new case is created.

I have written some code - which sends an email and includes some of the field values for a new case, but it leaves off the lookup field values (in other words, when I reference the Account.BillingCity, Contact.Email, etc.), I get null values.

From what I can tell, it looks like lookup values are inserted on new record creation after the case is inserted (and after the "After Insert" triggers have fired - this is why I get null values when the fields are referenced in my sent email.

I could use some advice on how to pull in these lookup values as part of my trigger - so I don't have a bunch of Null values for lookup fields.

Thanks in advance
Best Answer chosen by Scott Carlson
Scott CarlsonScott Carlson
Found my answer at https://developer.salesforce.com/page/An_Introduction_To_Email_Services_on_Force.com. I needed to include a query as part of the Trigger instantiation, and now it works:


for (Case myCase : [SELECT Id, Subject, CreatedDate, Product_Code__c, 
                      Owner.Name, Account.Name, Account.BillingCity,
                      Account.BillingStateCode, Contact.Name,
                      Contact.Email, Contact.Phone                      
                      FROM Case WHERE
                      Id IN :Trigger.new]) {
Instead of...


for (Case myCase : Trigger.new) {

Now it works!



All Answers

Scott CarlsonScott Carlson
// with code...

trigger CaseNotification on Case (after insert) {

  List<Messaging.SingleEmailMessage> mails = 
  new List<Messaging.SingleEmailMessage>();
  
  for (Case myCase : Trigger.new) {
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
      List<String> sendTo = new List<String>();
      sendTo.add('test@test.com');
      mail.setToAddresses(sendTo);
    
      mail.setReplyTo('noreply@salesforce.com');
      mail.setSenderDisplayName('Automated Case Notifcation');
    
      // Set email contents
      mail.setSubject('New Customer Complaint');
      String body = 'Subject: ' + myCase.Subject + '<br/>';
      body += 'Created Date: ' + myCase.CreatedDate + '<br/>';
      body += 'Product: ' + myCase.Product_Code__c + '<br/>';
      body += 'Owner: ' + myCase.Owner + '<br/>';
      body += '---------------------------------------------' + '<br/>' + '<br/>';
      body += 'Account Name: ' + myCase.Account + '<br/>';
      body += 'Account City: ' + myCase.Account.BillingCity + '<br/>';
      body += 'Account State: ' + myCase.Account.BillingStateCode + '<br/>';
      body += 'Contact Name: ' + myCase.Contact + '<br/>';      
      body += 'Contact Email: ' + myCase.Contact.Email + '<br/>';
      body += 'Contact Phone: ' + myCase.Contact.Phone + '<br/>' + '<br/>';
      
      mail.setHtmlBody(body);
      mails.add(mail);
  }
  Messaging.sendEmail(mails);
}
Scott CarlsonScott Carlson
Found my answer at https://developer.salesforce.com/page/An_Introduction_To_Email_Services_on_Force.com. I needed to include a query as part of the Trigger instantiation, and now it works:


for (Case myCase : [SELECT Id, Subject, CreatedDate, Product_Code__c, 
                      Owner.Name, Account.Name, Account.BillingCity,
                      Account.BillingStateCode, Contact.Name,
                      Contact.Email, Contact.Phone                      
                      FROM Case WHERE
                      Id IN :Trigger.new]) {
Instead of...


for (Case myCase : Trigger.new) {

Now it works!



This was selected as the best answer