Sweet.apex

  • Docs
  • Tutorials
Docs Menu
  • Sweet.apex Core
    • Transpilation
    • Grammar
    • Command
    • Config
  • Features
    • Action
    • Apex Doc
    • Array Creation
    • Aspect
    • Cast
    • Default Value
    • Enum
    • File
    • Function
    • Identity
    • Injection
    • Lambda
    • Log
    • Mod
    • Not Null
    • Operator
    • Optional
    • Reflection
    • Rethrow
    • Switch
    • Template String
    • Template
    • Script
    • Tagged String
    • Annotation
    • Nullable
    • Var
    • Val
    • Map Access
    • Constructor
    • Transaction
    • Destructure
    • Import Static
    • Pipeline
    • Varargs
    • Patch
    • Import As
  • Plugin Development
    • Feature
    • Test Case

Transaction Guide

Transaction

Feature Overview

This feature generates transactional methods that roll back when an exception is thrown.

Prerequisite

None

Sweet Apex Example

public class TransactionDemo {
    @transaction
    public void init() {
        // TODO
    }

    @transaction
    public static Integer sum() {
        return 0;
    }
}

Transpiled Apex

public class TransactionDemo {
    public void init() {
        Savepoint sp = Database.setSavepoint();
        try {
            transaction_init();
        }
        catch(Exception e) {
            Database.rollback(sp);
            throw e;
        }
    }
    public static Integer sum() {
        Savepoint sp = Database.setSavepoint();
        try {
            return transaction_sum();
        }
        catch(Exception e) {
            Database.rollback(sp);
            throw e;
        }
    }
    private void transaction_init() {
        // TODO
    }
    private static Integer transaction_sum() {
        return 0;
    }
}

Usage

@transaction will surround the original method with try-catch block so that it will roll back the transaction in case of exceptions.

Contribute on Github! Edit this section.