DB Transaction

Discovering the capability to modify data might lead you to scenarios where altering multiple entities simultaneously becomes necessary. Imagine wanting to add a new group and then immediately add a member to that group. This seems straightforward, until you encounter an error while adding the group member—perhaps due to validation issues or incorrect data. This would leave you with a new group but no members.

Modern applications address this challenge with the concept of transactions, which bundle changes together so that if one action fails, all changes are reverted, or 'rolled back'. Helix embraces this modern approach, recognizing the importance of supporting such scenarios. Frankly, we introduced transaction blocks partly out of convenience; it saves us the effort of manually writing Lava logic to undo changes when errors occur, ensuring the system remains efficient and user-friendly.

{% dbtransaction %}

    {% modifygroup id:'0' %}
        [[ property name:'Name' ]]Ted's Traveling Circus[[ endproperty ]]
        [[ property name:'GroupTypeId' ]]26[[ endproperty ]]
    {% endmodifygroup %}
    
    {% modifygroupmember id:'0' %}
        [[ property name:'PersonId' ]]{{ CurrentPerson.Id }}[[ endproperty ]]
        [[ property name:'GroupId' ]]{{ ModifyResult.Group.Id }}[[ endproperty ]]
    {% endmodifygroupmember %}
    
{% enddbtransaction %}

{% if TransactionResult.Success == false %}
    <div class="alert alert-warning">
        <strong>{{ TransactionResult.ErrorMessage }}</strong><br>
        Validation Messages<br>
        <ul>
        {% for message in TransactionResult.ValidationErrors %}
            <li>{{ message.ErrorMessage }}</li>
        {% endfor %}
        </ul>
    </div> 
{% endif %}

You might catch in the sample above that the call to save a group member (lines 8-11) is missing the required GroupRoleId. Because of this the previously saved Group is rolled back.

Database transactions are currently supported exclusively for encapsulating Modify Entity commands.

Last updated