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
pmbpmb 

Accessing a lookup data type in a trigger

Does anyone know the syntax to access a lookup data type in a trigger. I am trying to grab a Contact Id from a trigger that is created on a Contract insert.  Here is is the code:

trigger emailConfirm on Contract (after insert) {
Contract[] newContract = Trigger.new;
Id templateId= '00X401200012BvE';
Id targetId = newContract[0].CustomerSigned
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSubject('contract confirm');
mail.setTemplateId(templateId);
mail.setTargetObjectId(targetId);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

This does not work. I can grab any fields that are not of data type lookup though. For example newContract[0].ContractNumber works just fine.

I also tried the following with no success:

newContract[0].CustomerSigned.Id
newContract[0].CustomerSigned[0].Id


Any help is greatly appreciated.

PB
arnt72arnt72
I think the issue is here:
Id targetId = newContract[0].CustomerSigned

If I understand correctly, this gives you access to the ID, not the record as such. If you need access to the record, it needs to be returned by a query.

See APEX Reference here, 2nd paragraph: http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SObjects_field_relationships.htm

"The ID field can be used to change the account with which the contact is associated, while the sObject reference field can be used to access data from the account. The reference field is only populated as the result of a SOQL or SOSL query (see note below). "