/*
Consider two objects:- AKG__trans1__c and AKG__trans2__c
and AKG__trans1__c has field :- Amount (AKG__tr1Amount__c) field and
AKG__trans2__c has field :- AKG__tr2Amount__c
Suppose AKG__tr2Amount__c is depends on AKG__tr1Amount__c field, and if AKG__tr1Amount__c is null then it will be roll back insert query on AKG__trans1__c.
*/
/*------------------------- VF page Code -------------------------------------*/
<apex:page controller="savepointRollbackCon" tabStyle="Account">
<apex:pageMessages />
<apex:form >
<apex:pageBlock title="Transaction 1" >
<apex:pageblockbuttons >
<apex:commandButton value="SAVE!!" action="{!savemethod}"/>
</apex:pageblockbuttons>
<apex:pageblocksection title="Insert Trans1">
<apex:inputField label="Name" value="{!trans1obj.Name}"/>
<apex:inputField value="{!trans1obj.tr1Amount__c}"/>
</apex:pageblocksection>
</apex:pageBlock>
</apex:form>
</apex:page>
/*-------------------------------controller Code ---------------------------*/
public with sharing class savepointRollbackCon {
public AKG__trans1__c trans1obj{get;set;}
public AKG__trans2__c trans2obj{get;set;}
public savepointRollbackCon(){
trans1obj = new AKG__trans1__c();
trans2obj = new AKG__trans2__c();
}
public void savemethod(){
//Declare Savepoint
Savepoint sp = Database.setSavepoint();
insert trans1obj;
try{
trans2obj.Name= trans1obj.name;
trans2obj.tr2Amount__c= trans1obj.tr1Amount__c + 20;
insert trans2obj;
}
catch(Exception e){
// Use Rollback
Database.rollback(sp);
ApexPages.addMessage( new ApexPages.message(ApexPages.Severity.Error,' MY ERRRR '+e)) ;
}
}
}
Consider two objects:- AKG__trans1__c and AKG__trans2__c
and AKG__trans1__c has field :- Amount (AKG__tr1Amount__c) field and
AKG__trans2__c has field :- AKG__tr2Amount__c
Suppose AKG__tr2Amount__c is depends on AKG__tr1Amount__c field, and if AKG__tr1Amount__c is null then it will be roll back insert query on AKG__trans1__c.
*/
/*------------------------- VF page Code -------------------------------------*/
<apex:page controller="savepointRollbackCon" tabStyle="Account">
<apex:pageMessages />
<apex:form >
<apex:pageBlock title="Transaction 1" >
<apex:pageblockbuttons >
<apex:commandButton value="SAVE!!" action="{!savemethod}"/>
</apex:pageblockbuttons>
<apex:pageblocksection title="Insert Trans1">
<apex:inputField label="Name" value="{!trans1obj.Name}"/>
<apex:inputField value="{!trans1obj.tr1Amount__c}"/>
</apex:pageblocksection>
</apex:pageBlock>
</apex:form>
</apex:page>
/*-------------------------------controller Code ---------------------------*/
public with sharing class savepointRollbackCon {
public AKG__trans1__c trans1obj{get;set;}
public AKG__trans2__c trans2obj{get;set;}
public savepointRollbackCon(){
trans1obj = new AKG__trans1__c();
trans2obj = new AKG__trans2__c();
}
public void savemethod(){
//Declare Savepoint
Savepoint sp = Database.setSavepoint();
insert trans1obj;
try{
trans2obj.Name= trans1obj.name;
trans2obj.tr2Amount__c= trans1obj.tr1Amount__c + 20;
insert trans2obj;
}
catch(Exception e){
// Use Rollback
Database.rollback(sp);
ApexPages.addMessage( new ApexPages.message(ApexPages.Severity.Error,' MY ERRRR '+e)) ;
}
}
}