• Scott Carlson
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
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
I'm trying to learn about triggers in SF - and am working on my first task: a trigger that will set an Opportunity price book based on two Account field values.

GPO__c is a text field
GPO_Tier__c is a number

I'm trying to set the Opportunity Price Book based on these field values. Example - if the Account GPO is "GPO A" and the GPO Tier is 1, it should match a price book with the name of "GPO A Tier 1".

Here's some code I've put together so far:

trigger SetOppPriceBook4 on Opportunity (before insert) {
	for(Opportunity o : trigger.new){
        
        List<Account> acct = [SELECT GPO__c, GPO_Tier__c, Id FROM Account WHERE ID = :o.AccountId]; 
        List<Pricebook2> pb = [SELECT id from Pricebook2 WHERE Name = :acct[0].GPO__c + ' Tier ' + :acct[0].GPO_Tier__c];
    
        if(!pb.isEmpty()){
        	o.PriceBook2Id = pb[0].id;
        }
     
        else {o.Pricebook2Id = NULL;
        
        }
    }

}
I think I'm running into some problems with concatenating within the select statement. What do I need to change
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