1

With reference to the line of code (method createContact) below I am wondering where List candidateFromTrigger is created? This doesn’t seem to follow the create new list syntax

public void createContact(List<Candidate_c> candidateFromTrigger)

Is this statement (below) not required somewhere in the code to create the new List candidateFromTrigger

E.g List<candidate_c> candidateFromTrigger = new List <candidate_c>(); (I was expecting this kind of stament in the code. I am new to apex)

Goal of the code:

Access method from trigger by passing trigger.new to createContact

A.createContact(trigger.new)

2

Parameters are initialized from the caller. Consider this trivial example:

public Integer add(Integer a, Integer b) {
    return a + b;
}

Where is "a" and "b" initialized? From wherever the function is called. For example:

Integer c = add(5, 10); // c is 15
Integer d = 7;
Integer e = 8;
Integer f = add(d, e); // f is 15
Integer g;
Integer h = add(d, g); // NullPointerException here: g has a null value

For triggers, Trigger.new is automatically defined for most cases (except for delete events). It is automatically a list of whatever type of trigger is being called. For example, given trigger X on Account, Trigger.new is List<Account>. Inside classes, Trigger.new is either null, if you're not in a trigger context, or List<SObject> if you're currently in a trigger context.

  • Thanks!! You explained this very clearly with your example. Thanks a ton!! – Think First Feb 16 '15 at 1:50
1

Trigger.new is the List. It is a trigger context variable that is implicitly available, i.e., it isn't declared anywhere.

Trigger.new is defined as:

new
Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.

public void createContact(List<Candidate_c> candidateFromTrigger) is a method in which the candidateFromTrigger is a parameter defined as a List<Candidate__c>. The method createContact is invoked with the code A.createContact(Trigger.new). The Trigger.new is the argument to the method and it is actually a List<Candidate__c>.

  • Thx for your response. Is candidateFromTrigger also a list which gets the values passed by trigger.new? If yes then why dont we need to declare candidateFromTrigger as a list following normal list creating syntax? Like this: List<candidate_c> candidateFromTrigger = new List <candidate_c>() – Think First Feb 16 '15 at 0:37
  • Sure @ThinkFirst! Glad it is clear now! :) – Peter Knolle Feb 16 '15 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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