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
CvrKCvrK 

Does Trigger.New works for After triggersin any way ?

 I read that trigger.new does not work for after insert/update triggers but i have seen in many examples where trigger.new is used in after triggers,so wondering if trigger.new works or not for after events in any situation.

below example trigger for the reference that used trigger.new for After Insert event .

trigger AutoOpp on Account(after insert) {
  List<Opportunity> newOpps = new List<Opportunity>();
  for (Account acc : Trigger.new) {
    Opportunity opp = new Opportunity();
    opp.Name        = acc.Name + ' Opportunity';
    opp.StageName   = 'Prospecting';
    opp.CloseDate   = Date.today() + 90;
    opp.AccountId   = acc.Id; // Use the trigger record's ID
    newOpps.add(opp);
  }
  insert newOpps;
}
Best Answer chosen by CvrK
Abhishek BansalAbhishek Bansal
Hi ,

Trigger.new is available in after events but the records that are stored in this new list are only available in read only mode.
You can not perform any DML on these record since they are in read-only mode.
So they are available in after events but in read only mode only.

I think this is what you want to know.
If you still have any doubts regarding this than you can ask.

Let me know if you need more information on this.

Thanks,
Abhishek 

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
1) isExecuting Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or anexecuteanonymous() API call.
2) isInsert Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or theAPI.
3) isUpdate Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or theAPI.
4) isDelete Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or theAPI.
5) isBefore Returns true if this trigger was fired before any record was saved.
6) isAfter Returns true if this trigger was fired after all records were saved.
7) isUndelete Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)
8) 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.
9) newMap A map of IDs to the new versions of the sObject records. Note that this map is only available in before update, after insert, and after update triggers.
10) old Returns a list of the old versions of the sObject records.Note that this sObject list is only available in update and delete triggers.
11) oldMap A map of IDs to the old versions of the sObject records.Note that this map is only available in update and delete triggers.
12) size The total number of records in a trigger invocation, both old and new.

Please check below post. I hope that will help you
http://amitsalesforce.blogspot.in/2015/10/trigger-context-variables.html
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Please let us know if this will help you

Thanks
Amit Chaudhary
Abhishek BansalAbhishek Bansal
Hi ,

Trigger.new is available in after events but the records that are stored in this new list are only available in read only mode.
You can not perform any DML on these record since they are in read-only mode.
So they are available in after events but in read only mode only.

I think this is what you want to know.
If you still have any doubts regarding this than you can ask.

Let me know if you need more information on this.

Thanks,
Abhishek 

 
This was selected as the best answer
ManojjenaManojjena
Hi CvrK,
Abhishek and amit are correct after event trigger trigger.new is availabel in read only mode .Directly you can not assign any value liek before event as the record is saves to data base .

Basically in after context always you will get the id of the record .Trigger.new is a list of records.So you need to collect the set of id and query from database and do what ever changes you need to  why because teh trigger .new is ready only mode which is already posted by Abhishek .Finnaly you need to update the list what you have queried .

You can test all avaiability of context variable in different context .Please check the example given below link create this trigger and create and update and delete one record and observe the debug log .

http://manojjena20.blogspot.in/2013/03/manojs-trigger-blog.html

Let us know still you have any doubt .
Thanks
Manoj

 
CvrKCvrK
Thanks for your time and such a clear explanation guys...