Author Archives: Biswajeet

About Biswajeet

Biswajeet is my Name, Success is my Aim and Challenge is my Game. Risk & Riding is my Passion and Hard Work is my Occupation. Love is my Friend, Perfection is my Habit and Smartness is my Style. Smiling is my Hobby, Politeness is my Policy and Confidence is my Power.

Salesforce: Use Tab in Visualforce Page

Visualforce Page:

<apex:page doctype="html-5.0" standardcontroller="account" tabstyle="account">
    <apex:pageblock title="{!account.Name}">
        <apex:tabpanel id="tabpanel">
         
            <apex:tab label="Account Details" rerender="tabpanel" switchtype="ajax">
            <apex:detail relatedlist="false"></apex:detail>
            </apex:tab>
             
            <apex:tab label="Contacts" rerender="tabpanel" switchtype="ajax">
            <apex:relatedlist list="Contacts"></apex:relatedlist>
            </apex:tab>
             
            <apex:tab label="Opportunities" rerender="tabpanel" switchtype="ajax">
            <apex:relatedlist list="Opportunities"></apex:relatedlist>
            </apex:tab>
             
            <apex:tab label="Cases" rerender="tabpanel" switchtype="ajax">
             <apex:relatedlist list="Cases"></apex:relatedlist>
            </apex:tab>
             
        </apex:tabpanel>
    </apex:pageblock>
</apex:page>

Note: Please pass account id in url e.g.
https://salesforce.com/apex/TabInVisualForcePage?id=0019000001DEV5z

Output:

index

Transaction Control using SavePoint and Rollback in Salesforce

SavePoint and Rollback will help us to maintain transaction for DML statement.
Suppose you  have written multiple lines of DML statements in a try block, If any error occurs during DML Operations, the operation will be rolled back to the most recent save point and the entire transaction will not be aborted.

Savepoint sp;
try{
   sp = Database.setSavepoint();
 
   Account a = new Account();
   a.Name = 'Test Account';
   insert a;
 
   Contact c = new Contact(Account = a.Id);
   c.FirstName = 'Biswajeet';
   c.LastName = 'Samal';
   insert c;
  }
catch(Exception e){
    Database.RollBack(sp);
}

In this example, if any error occurs while inserting the Account ‘a’ or Contact ‘c’, then the entire transaction will be rolled back to SavePoint ‘sp’, as specified in the catch section by Database.Rollback method.

Checkbox Options in Salesforce

Visualforce Page:

<apex:page controller="SampleController">
    <apex:form>
        <!--Country Checkboxes-->
        <apex:selectCheckboxes value="{!countries}">
            <!--Country List-->
            <apex:selectOptions value="{!CountryList}"/>
        </apex:selectCheckboxes><br/>
        <apex:commandButton value="Submit" action="{!getSelectedCountries}" rerender="out" status="status"/>
    </apex:form>
    <!--Selected Countries Output-->
    <apex:outputPanel id="out">
        <apex:actionstatus id="status" startText="Loading...">
            <apex:facet name="stop">
                <apex:outputPanel>
                    <p>Selected Countries:</p>
                    <apex:dataList value="{!countries}" var="c">{!c}</apex:dataList>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
</apex:page>

Apex Class:

public class SampleController {
    
    Public List<string> countries {get;set;}
    
    public SampleController(){
        countries = new List<string>();
    }
    
    public List<SelectOption> getCountryList() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('USA','USA'));
        options.add(new SelectOption('INDIA','India'));
        options.add(new SelectOption('CANADA','Canada'));
        options.add(new SelectOption('MEXICO','Mexico'));
        return options;
    }
    
    public pagereference getSelectedCountries(){
        system.debug('selectedcountries -' + countries);
        return null;
    }
}

Output:

To display checkbox options in vertical direction, use layout property layout="pageDirection" in selectCheckboxe component.

<apex:selectCheckboxes value="{!countries}" layout="pageDirection">
    <apex:selectOptions value="{!CountryList}"/>
</apex:selectCheckboxes>

Salesforce: Convert decimal value to integer using apex

If we convert decimal into integer like below code, it will compile without a problem, but at the time of execution, an error will occur.

Decimal mydecval = 15.0;
Integer myintval = Integer.valueOf(mydecval);

So, we should always use decimalvariable.intValue() to convert decimal value into integer.
Here is sample code:

Decimal mydecval = 15.0;
Integer myintval = mydecval.intValue();