Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I'm building this trigger on a custom object and want to access the fields in that custom object directly. It won't let me and says: "Error: Compile Error: Variable does not exist: ".

trigger myTrigger on Timecard__c (after insert) {
  string s = Notes__c;

}
share|improve this question

3 Answers 3

up vote 3 down vote accepted

Triggers in Apex operate on batches (possibly size 1) of records. There are trigger context variables that hold the records to be processed by the trigger and there are considerations to be aware of when using them. I strongly suggest reading more about Triggers in the Apex Developer's Guide. There are a host of other considerations such as the order of execution and bulkifying that are worth knowing about.

The context variables are as follows. Note that there availability depends on what operation (e.g., after insert, before insert) caused the trigger to execute.

Trigger.new - Returns a list of the new versions of the sObject records.

Trigger.newMap - A map of IDs to the new versions of the sObject records.

Trigger.old - Returns a list of the old versions of the sObject records.

Trigger.oldMap - A map of IDs to the old versions of the sObject records.

trigger myTrigger on Timecard__c (after insert) {
  for (Timecard__c timeCard : Trigger.new) {
       string s = timeCard.Notes__c;
  }
}
share|improve this answer
    
great, exactly what i needed. –  user988 Jan 5 '13 at 0:56

do like this -

trigger myTrigger on Timecard__c (after insert)    
{
For (Timecard__c timeCard : trigger.new)
string s = timeCard.Notes__c;
}
share|improve this answer

You need to reference the field name as follows

trigger myTrigger on Timecard__c (after insert)    
{
For (Timecard__c timeCard : trigger.new)
string s = timeCard.Notes__c;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.