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
DbjensenDbjensen 

Pass trigger.new and trigger.old to a single list

Hello - I'm trying to pass trigger.new and trigger.old to a single list which then will be passed to a class.

Here's the class I'm calling in the trigger: 
LeadOwnerAssignment.assignOwner(Trigger.new, Trigger.old);
Here is the class method I'm passing the lists to:
public static void assignOwner(List<Lead> newLead, List<Lead> oldLead) {

I'm hoping to do something like this (except pass both trigger.new and trigger.old).
List<Lead> newLeadList = new List<Lead>();
                    for(Lead newLead : Trigger.new){
                        if(newLead.Lead_Id__c == null){
                            newLeadList .add(newLead);
                        }  
                    }
                   LeadOwnerAssignment.assignOwner(newLeadList );
 
Any help would be greatly appreciated. 
Best Answer chosen by Dbjensen
HARSHIL U PARIKHHARSHIL U PARIKH
Hello Dbjensen,

Yes,  you can indeed pass your Trigger.New and Trigger.Old directly lists inside your class method. I would say you don't need to loop through the each record. Below as an example of an approach you may want to consider,

Trigger Code:
Trigger AccountObjTrigger on Account (Before Insert) {
    
    If(Trigger.IsBefore && Trigger.IsInsert)
    {
        AccountTriggerHandler.Method_1(Trigger.New, Trigger.Old);
        AccountTriggerHandler.Method_2(Trigger.NewMap, Trigger.OldMap);
    }
}

Handler Class with few methods:
 
Public class AccountTriggerHandler {
    
    Public static void Method_1(List<Account> newValues, List<Account> oldvalues)
    {
        For(Account EachAccount : newvalues)
        {
            // Do something
        }
    }
    
    Public static void Method_2(Map<Id, Account> newValuesMap, Map<Id, Account> oldValuesMap)
    {
        For(Account EachAccount : newValuesMap.values())
        {
            // Do something
        }
    }

}

Hope this helps!

If this resolves your query then please mark it as Best Answer!

Regards,
Harshil Parikh


 

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
Hello Dbjensen,

Yes,  you can indeed pass your Trigger.New and Trigger.Old directly lists inside your class method. I would say you don't need to loop through the each record. Below as an example of an approach you may want to consider,

Trigger Code:
Trigger AccountObjTrigger on Account (Before Insert) {
    
    If(Trigger.IsBefore && Trigger.IsInsert)
    {
        AccountTriggerHandler.Method_1(Trigger.New, Trigger.Old);
        AccountTriggerHandler.Method_2(Trigger.NewMap, Trigger.OldMap);
    }
}

Handler Class with few methods:
 
Public class AccountTriggerHandler {
    
    Public static void Method_1(List<Account> newValues, List<Account> oldvalues)
    {
        For(Account EachAccount : newvalues)
        {
            // Do something
        }
    }
    
    Public static void Method_2(Map<Id, Account> newValuesMap, Map<Id, Account> oldValuesMap)
    {
        For(Account EachAccount : newValuesMap.values())
        {
            // Do something
        }
    }

}

Hope this helps!

If this resolves your query then please mark it as Best Answer!

Regards,
Harshil Parikh


 
This was selected as the best answer
Raj VakatiRaj Vakati
Hi ,, 

Please read my comments here .. You can define a method to accept one list which contains both the old and new values .. its not a big deal .. but the problem here is how you will be able to identify which required is old and new .. to find the old and new in one list is challenge .


My opinion is you want to pass one Argument .. you can do it like that .. define the assignOwner method accept the Map<boolean,list<Account>>

                   LeadOwnerAssignment.assignOwner(new Map<Id,List<Account>>);

If the boolean value is true --> Consider the value as a new records 
if the boolean value is false -- > consider the value as a old records 
 
DbjensenDbjensen
Hi Harshil  - Thanks so much for the helpful info. This is what I was looking for. I appreciate the example. We very helpful.
DbjensenDbjensen
HI Raj - Thanks for the reply. This is helpful info. I appreciate it.