10

I want to get data from children objects and populate field at parent object. I used after insert & update trigger to get the information.

If I run a query on inserted/ updated object in a trigger will it pull in the information of the inserted/ updated record?

From the reference the order of execution of triggers code is:

.... 
5. Saves the record to the database, but doesn't commit yet.
6. Executes all after triggers.
....

Could someone please explain what means saving record to database & why what's different from committing? And what will happen if I run a query on inserted & updated object in the trigger?

1 Answer 1

14

Step 5 refers to a temporary database save; the rows are saved to the database, but the transaction isn't committed. When data is saved to the database, it becomes available to the current transaction. However, a commit is what finalizes the data and releases the lock that allows other transactions to also see that data.

In terms of raw SQL commands, each life cycle basically behaves like:

START TRANSACTION
// Perform validations, etc
// Now perform before-DML triggers
INSERT/UPDATE    // This is "Step 5" in the life cycle
// Now perform after-DML triggers // Step 6
// Now perform workflows, etc
// If we have errors, we...
ROLLBACK
// Otherwise we...
COMMIT

This "transaction isolation" is what allows the database to see a single, consistent view of the data in the system. No other transaction can see "in-flight" data, which could result in incorrect calculations, etc.

In other words, once the INSERT or UPDATE function has been called (salesforce performs "soft" deletes, so rows aren't purged until sometime later using DELETE), the current transaction's view of the system is different than the rest of the system. It can see records just inserted and updates that have occurred in the current transaction, before the rest of the system gets to see it. If the transaction fails (rolls back), then no other transaction will ever see what transpired within that transaction.

5
  • 1
    It's also worth noting that on Step 5 when the record is committed to the database, it receives an Id and can then be referenced in SOQL, as you noted.
    – greenstork
    Sep 11, 2014 at 16:27
  • Careful with the terminology. It's saved, not committed. Committing is a permanent act, while saving is just temporary and reversible.
    – sfdcfox
    Sep 11, 2014 at 16:30
  • yep, noted, you are correct
    – greenstork
    Sep 11, 2014 at 16:43
  • @sfdcfox I know this is really old, lol, but what can you do if there is a delete? As per what you said, the query will still return the deleted records. Other than comparing the Id values in Trigger.old with the results in the query and manually removing them, is there any other way for this?
    – hamayoun
    Feb 21 at 23:54
  • @hamayoun I'm not sure what you're asking. Could you describe the scenario better? Also, you could ask a new question so you have more space. I'd be happy to take a look at it.
    – sfdcfox
    Feb 22 at 0:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .