Apex Transactions | Techila
single,single-post,postid-2399,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive

Apex Transactions


Apex Transactions

An Apex transaction represents a set of operations that are executed as a single unit. All DML operations in a transaction either complete successfully, or if an error occurs in one operation, the entire transaction is rolled back and no data is committed to the database. The boundary of a transaction can be a trigger, a class method, an anonymous block of code, a Visualforce page, or a custom Web service method.

All operations that occur inside the transaction boundary represent a single unit of operations. This also applies for calls that are made from the transaction boundary to external code, such as classes or triggers that get fired as a result of the code running in the transaction boundary. For example, consider the following chain of operations: a custom Apex Web service method causes a trigger to fire, which in turn calls a method in a class. In this case, all changes are committed to the database only after all operations in the transaction finish executing and don’t cause any errors. If an error occurs in any of the intermediate steps, all database changes are rolled back and the transaction isn’t committed.

How are Transactions Useful ?

Transactions are useful when several operations are related, and either all or none of the operations should be committed. This keeps the database in a consistent state. There are many business scenarios that benefit from transaction processing. For example, transferring funds from one bank account to another is a common scenario. It involves debiting the first account and crediting the second account with the amount to transfer. These two operations need to be committed together to the database. But if the debit operation succeeds and the credit operation fails, the account balances will be inconsistent.


This example shows how all DML insert operations in a method are rolled back when the last operation causes a validation rule failure. In this example, the invoice method is the transaction boundary—all code that runs within this method either commits all changes to the platform database or rolls back all changes. In this case, we add a new invoice statement with a line item for the pencils merchandise. The Line Item is for a purchase of 5,000 pencils specified in the Units_Sold__c field, which is more than the entire pencils inventory of 1,000. This example assumes a validation rule has been set up to check that the total inventory of the merchandise item is enough to cover new purchases.

Since this example attempts to purchase more pencils (5,000) than items in stock (1,000), the validation rule fails and throws an exception. Code execution halts at this point and all DML operations processed before this exception are rolled back. In this case, the invoice statement and line item won’t be added to the database, and their insert DML operations are rolled back.

In the Developer Console, execute the static invoice method.

// Only 1,000 pencils are in stock.

// Purchasing 5,000 pencils cause the validation rule to fail,

// which results in an exception in the invoice method.

Id invoice = MerchandiseOperations.invoice(‘Pencils’, 5000, ‘test 1’);

This is the definition of the invoice method. In this case, the update of total inventory causes an exception due to the validation rule failure. As a result, the invoice statements and line items will be rolled back and won’t be inserted into the database.

public class MerchandiseOperations {

    public static Id invoice( String pName, Integer pSold, String pDesc) {

        // Retrieve the pencils sample merchandise

        Merchandise__c m = [SELECT Price__c,Total_Inventory__c

            FROM Merchandise__c WHERE Name = :pName LIMIT 1];

        // break if no merchandise is found

        System.assertNotEquals(null, m);

        // Add a new invoice

        Invoice_Statement__c i = new Invoice_Statement__c(

            Description__c = pDesc);

        insert i;

        // Add a new line item to the invoice

        Line_Item__c li = new Line_Item__c(

            Name = ‘1’,

            Invoice_Statement__c = i.Id,

            Merchandise__c = m.Id,

            Unit_Price__c = m.Price__c,

            Units_Sold__c = pSold);

        insert li;

        // Update the inventory of the merchandise item

        m.Total_Inventory__c -= pSold;

        // This causes an exception due to the validation rule

        // if there is not enough inventory.

        update m;

        return i.Id;



Courtesy :


“Explore – Techila Global Services, A Salesforce development company”

Author: techila

No Comments

Post A Comment