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
Shane K 8Shane K 8 

Apex trigger code question

We have this code in our org for a trigger. I am trying to understand the code. Can anyone please explain what's being done in the bolded lines?
 
public with sharing class ObjAction {
    private Obj__c[] newObj;  
    private Obj__c[] oldObj; 
    ObjHandler triggerHandler = new ObjHandler();
    public ObjAction(Obj__c[] oldObj, Obj__c[] newObj){    
        this.newObj = newObj;    
        this.oldObj = oldObj;   
    } 
}

Thanks.
Best Answer chosen by Shane K 8
AmitSoniAmitSoni
// This is public constructor of the class ObjAction which is taking the 2 parameters as input 
public ObjAction(Obj__c[] oldObj, Obj__c[] newObj)
{
// This is assigning the parameter value to the private varibale defined in the class private Obj__c[] newObj;
this.newObj = newObj;
// This is assigning the parameter value to the private varibale defined in the class private Obj__c[] oldObj;
this.oldObj = oldObj;
}

These private variable can be referenced with in the class.

Hope this helps!!

All Answers

AmitSoniAmitSoni
// This is public constructor of the class ObjAction which is taking the 2 parameters as input 
public ObjAction(Obj__c[] oldObj, Obj__c[] newObj)
{
// This is assigning the parameter value to the private varibale defined in the class private Obj__c[] newObj;
this.newObj = newObj;
// This is assigning the parameter value to the private varibale defined in the class private Obj__c[] oldObj;
this.oldObj = oldObj;
}

These private variable can be referenced with in the class.

Hope this helps!!
This was selected as the best answer
Shane K 8Shane K 8
Thank you Amit. It helps. One more question: Is there any significance of using square brackets []? I normally see () brackets being used in code.
Abhishek BansalAbhishek Bansal
Hi Shane,

public ObjAction(Obj__c[] oldObj, Obj__c[] newObj){
This is the constructor of the class which takes two arguements. As the name suggests, the first parameter holds the list of old records and the second one holds the list of new records.

this.newObj = newObj;
This assigns the list of new records to the class variable.

this.oldObj = oldObj;
This assigns the list of old records to the class variable.

significance of using square brackets [] -> This represent the array or list of particular data type. In this case it holds the list of Obj__c records.
It is equivalent to List<Obj__c>.

Let me know if you have any more questions.

Thanks,
Abhishek Bansal.
AmitSoniAmitSoni
[ ] bracket -  represents the array of particular datatype. This is equivalent to List.
Obj__c[ ] newObj equivalent to List<Obj__c> newObj

Hope this clarifies!!