Anyone who have used SQL must be knowing about the Commit, Savepoint and Rollback operations. These operations help us control the transaction of the system. When we put a savepoint somewhere it creates a checkpoint, and then we can always rollback to the previous checkpoint whenever we feel like. Commit on the other hand saves the entire transaction. Rollback after a commit is not possible. This much we all know, however does this concept exist in Salesforce? We know that Salesorce has it’s own version of SQL, called SOQL. This works similarly like the SQL that we have used. In this version also we have transaction control to create savepoint. This is what we will discuss about transaction control in APEX.

APEX code

public class transectioncontrol {

    public void testcontrol(){
        Account a = new Account(Name = 'testaccount');
        insert a;

        Savepoint savepnt = Database.setSavepoint();

        a.AccountNumber = '123';
        update a;

        Database.rollback(savepnt);
    }
}

As you can see that we have created a simple class and then performed insert and upsert operations. The first operation executes normally, however before the upsert operation we have created a savepoint whose reference we have kept in savepnt variable of the savepoint class. After the upsert operation when we will call the rollback function the changes made by the upsert will disappear.

VF page code

<apex:page controller="transectioncontrol">
    <apex:form>
        <apex:commandButton action="{!testcontrol}" value="test"/>
    </apex:form>
</apex:page>

As you can see that there is nothing in the visualforce page, but a button to call the function to perform the desired operation. Once you have created this page, open the page and click on the button. After that an account will be visible with the name testaccount. However the account number will not be visible on that account.

Support

That’s all about creating a post on chatter through APEX, for any further queries feel free to contact us at:

https://wedgecommerce.com/contact-us/

Or let us know your views on how to make this code better, in comments section below.

Leave A Reply

Please verify that you are not a robot.

Tell us about Your Company

How can we help you with your business?




    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home