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
MrBungleMrBungle 

Passing Trigger.new to an apex class

I want to tidy up my triggers and follow a best practice I learned of at Dreamforce X. I learned that is is better to have just one trigger of one type (Before Insert, Before Update.. etc.). This way you can have control over the order of execution and just makes for less clutter.

 

I am trying to move all my trigger code to apex class methods and just call them from the trigger passing in the trigger.new, trigger.old objects (where applicable) as arguments. Seems easy and I've seen many articles on-line on this very subject. The problem is none of these articles show me how to use the trigger object that is passed to the method. Just the template. I need to iterate through the trigger in the method to make it work but in the for loop I get a save error when trying to save the class: (Save Error: Loop variable must be of type Object). Can someone help me figure out why? Thanks!

 

I get the error on line 18: "for (Asset a : newTrigger)" of the following code example.

 

public with sharing class My_TriggerHandler 
{

    private boolean m_isExecuting = false;
    
    public My_TriggerHandler(boolean isExecuting)
    {
    	m_isExecuting = isExecuting;
    }

    public boolean IsTriggerContext
    {
        get {return m_isExecuting;}
    }

    Public void AssetBeforeInsert(object[] newTrigger)
    {
        for (Asset a : newTrigger) 
        {

        }
    }

}

 

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

You have to cast the generic object back into it's actual sObject type e.g.

 

public void AssetBeforeInsert(object[] newTrigger) {


List<Asset> newassets = (List<Asset>) newTrigger;

 

for (Asset a : newassets)  {

//Now you can refer to the asset objects as usual

}

 

}

All Answers

SammyComesHereSammyComesHere

Store the Trigger.New as a List.

 

List<Contact> lContactList = Trigger.New;

 

Pass this list as a parameter to your method.

 

 

BritishBoyinDCBritishBoyinDC

You have to cast the generic object back into it's actual sObject type e.g.

 

public void AssetBeforeInsert(object[] newTrigger) {


List<Asset> newassets = (List<Asset>) newTrigger;

 

for (Asset a : newassets)  {

//Now you can refer to the asset objects as usual

}

 

}

This was selected as the best answer
MrBungleMrBungle

Thank you both for your replies. Very helpful!

MrBungleMrBungle

Question: When working with trigger.new in a trigger it is read-only because the records contained are not committed to the DB yet. Because of this I cannot assign a value to a field in the trigger.new. I learned that when I pass trigger.new as an arg it isn't read-only any more. Is there a way to specifiy that the arg is by reference so that I can perform actions such as .addError in my apex method handling my trigger code? Thanks!

BritishBoyinDCBritishBoyinDC

You can update values in trigger.new in before calls (before insert, before update) but not in after calls.