0
  1. Upsert on the apex controller and it has both insert and update records.
  2. Trigger on before update, before insert
  3. A trigger has a recursion check in the starting.

Question: Trigger will fire first for Insert and then update or vice-versa and can we control the flow in terms of insert and update.

0

I dont think , there is a standard functionality to control the flows on upsert operation but you can simply, divide your list into update and insert list and do the dmls separately with your desired order.

list<Sobject> updateList = new list<Sobject>();
list<Sobject> insertList = new list<Sobject>();

Database.update(updateList);
Database.update(insertList);
//or
//Database.update(insertList);
//Database.update(updateList);
1
  • Thanks for the reply erkan, Here the problem is I don't have a control on the apex controller because it's a part of managed package. Hence this solution won't work in my scenario. – Lokesh Kumar Jun 20 '17 at 6:33
0

Well, in my understanding, both the insert and the update will be fired the moment in which they are invoked, that is, if you have first the insert and then the update, they will be invoked in that order. Furthermore, if your initial insert then fires other triggers, processes, etc. then the update will still not ocurr until they have all concluded. In other words, Apex is synchronous, unlike javascript, for example.

Something different is the concept of transaction, or execution context. Take into account that all those operations will abide the Governor Limits within the single transaction they are in, regardless of the amount of triggers fired afterwards. Read here.

At the end of the day, imagine your transaction like a tree: your first DML might invoke other triggers, but the following dml won't be run until all those other triggers and operations have finished, all the way up to your initial DML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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