Transaction vs Transaction Group

Briefing:
I have read quite a few staff regarding transactions and seen quite a few scripts, and people do various staff which is not always straight forward. I thought it would be easier to ask here in this forum, rather than guessing and proceeding with the wrong option.

Question(s):

  1. What’s the main difference between a transaction and a transaction group?

  2. For example, if I update one element, let’s say, set one parameter of one element I need a transaction. But what if I have “for” loop (speaking python language here) that’s going through a list and update a lot of elements, would I need to use a group transaction?

  3. Does it depend on the complexity of a loop? Let’s say, I have 2 or more lists (in a “for” loop) that I would like elements to be updated in?

Please advise.

I’d say for 95% of cases, a single transaction is sufficient. I’ve only need to use a transaction group a couple of times, and that is for cases when I need to commit a transaction early in a script in order to do something else later with another transaction, but I want it all under a single undoable transaction group.

For looping, just put your transaction before the loop and commit it after so it registers as a single transaction. Most of the time you wouldn’t want the transaction inside of the for loop. Basically, something like this:

t = DB.Transaction(doc, "MyTool")
t.Start()

for i in my_loop:
    #do stuff

t.Commit()
1 Like

and a pyrevit way

from pyrevit import revit
with revit.Transaction('name your transaction'):
   for i in my_loop:
       #do stuff
2 Likes