Category Archives: Salesforce

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();

How to get default value of a picklist in apex controller?

String defaultVal = '';
Schema.DescribeFieldResult objDF = Lead.Status.getDescribe();
List<schema.picklistentry> pickVals = objDF.getPicklistValues();        
for (Schema.PicklistEntry pv: pickVals) {
    if (pv.isDefaultValue()) {
        defaultVal = pv.getValue();
    break;
    }    
}
system.debug('Picklist Default value is' + defaultVal);

How to Print a Visualforce page in Salesforce?

Controller:

public class PrintSample {
 
    public List<account> objList {get; set;}
    
    public PrintSample() {
        objList = new List<account>();
        objList = [SELECT Name, Phone FROM Account];
    }
}

Visualforce Page:

<apex:page showheader="false" controller="PrintSample">
    <apex:panelgrid width="100%" style="text-align:right;">
        <apex:form>
            <apex:commandlink value="Print" onclick="window.print();"></apex:commandlink>
        </apex:form>
    </apex:panelgrid>
    <apex:pageblock title="Account Information">
        <apex:pageblocktable value="{!objList}" var="a">
            <apex:column value="{!a.Name}"></apex:column>
            <apex:column value="{!a.Phone}"></apex:column>
        </apex:pageblocktable>
    </apex:pageblock>
</apex:page>

Output:

download