2

I want to create a collection (List or Set) which can behave like Trigger.New context. Means If we use the collection out side of it's class, we should not be able to add any element in this collection. However we can change content of its existing element.

For example in Trigger.New collection of Account Trigger, I can change name of the new Account as below, but But if I try to add any element in Trigger.New collection, It will give compile time error.

trigger accountTest on Account (before insert) 
{

    for(Account currAccount : Trigger.New)
    {
        currAccount.Name = 'Name changed'; // here I can change the name for new account.
    }

    Account tempAccount = new Acccount(Name='New Account to Add');

    // But if I try to add tempAccount in Trigger.New collection, It will give compile time error as below

    Trigger.New.add(tempAccount);  //Collection is read-only
}

Is this requirement possible? IF so, how can I achieve it?

closed as off-topic by Markus Slabina, Martin Lezer, Raul, Mohith Shrivastava, battery.cord Jun 29 '17 at 17:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Markus Slabina, Martin Lezer, Raul, Mohith Shrivastava, battery.cord
If this question can be reworded to fit the rules in the help center, please edit the question.

  • You may use a static list and call initialize method once initialised its size will be a constant value. – Avijit Chakraborty Jun 29 '17 at 13:09
  • 1
    I am surprised this question is receiving close votes. Seems very clear and interesting imo. – Adrian Larson Jun 29 '17 at 14:26
1

There are several "magic" data types that we can't create in Apex Code. For example, we can't create read-only SObject records (as Trigger.new in an after trigger), lists that can't be modified (as in Trigger.new), and so on. You could write your own class to emulate this, but it wouldn't be a perfect replica, since we can't create custom array classes like we can in Java.

0

Just assign Trigger.new to an Account list and manipulate that one instead.

List<Account> accounts = new List<Account>();
accounts.addAll(Trigger.new);
Account tempAccount = new Account(Name='New Account to Add');
accounts.add(tempAccount);
  • It was not the requirement. The requirement is to create a collection which is replica of Trigger.New collection. Which should not allow to add any element but should allow to make changes in existing elements. – Tikam Sangwani Jun 30 '17 at 5:27
  • A weird requirement. Yeah, you would have to create your own logic to manage that behavior. – Adam M Jul 1 '17 at 18:51
0

Trigger.New will going to give a single record or list of records on the object on which you've created a Trigger.

Here you're iterating for(Account currAccount : Trigger.New) and hence you've taken those record as a Single record in Account object.

List<Account> accountList = new List<Account>();
accounts.addAll(Trigger.new);
accountList.add(new Account(LastName='My LastName'));

Note: Trigger.New or Trigger.Old, you get those record in list as a standard practice.

  • I don't think this answer really addresses the original question. – Adrian Larson Jun 29 '17 at 14:32
  • @Adrian Larson, Agree with you. The original requirement is completely different. – Tikam Sangwani Jun 30 '17 at 5:31
  • @javaHelper The requirement is to create a collection which is replica of Trigger.New collection. Which should not allow to add any element but should allow to make changes in existing elements. Thanks – Tikam Sangwani Jun 30 '17 at 5:31

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