Tag Archives: Salesforce

Shortcut to a List View in Salesforce

Here is a quick way to get to a list view without having to go looking for the tab or in the case that you don’t have a tab you can use this trick to get to a list view screen.

Here is one record of Account object is “Cadinal Inc”, and I want to go to the Account List View.

Once you’re on the record, trim off everything after the first three characters after the first slash.

My full URL is https://biswajeet-dev-ed.my.salesforce.com/0019000001EJPPH. I’m going to trim off the 9000001EJPPH and be left with https://biswajeet-dev-ed.my.salesforce.com/001. Once down run that link and you will go to the list view page for that object.

This will work with all standard and custom objects.

Picklist Without None Value in Visualforce Page

Controller:

public class Sample{
    
    public Account acc {get;set;}
    public List<SelectOption> typeOptions {get;set;}
    
    // Constructor called when page is accessed.
    public Sample() {
        
        acc = new Account();        
        typeOptions = new List<SelectOption>();
        
        //Use DescribeFieldResult object to retrieve status field.
        Schema.DescribeFieldResult typeFieldDescription = Account.Type.getDescribe();
        
        //For each picklist value, create a new select option
        for (Schema.PickListEntry pl:typeFieldDescription.getPicklistValues()){
            typeOptions.add(new SelectOption(pl.getValue(),pl.getLabel()));
            
            //Obtain and assign default value
            if (pl.defaultValue){
                acc.Type= pl.getValue();
            }  
        }     
    }
}

Visualforce Page:

<apex:page controller="Sample">
    Please select account type:
    <br/>
    <apex:form >
        <apex:selectList size="1" value="{!acc.Type}">
            <apex:selectOptions value="{!typeOptions}"/>
        </apex:selectList>
    </apex:form>
</apex:page>

Output:

Export Data In Excel Using Visualforce Page

Here in below example I have a VF Page “AccountDetails.vf” with parameter of account Id. In that VF Page I’m showing the Account record respective Contacts. And a button that export the Contact records to excel sheet. Another VF Page “ContactExportToExcel.vf” for excel.

Viualforce Page: AccountDetails.vf

<apex:page standardController="Account" extensions="ContactExportToExcel">
    <apex:form>
        <apex:pageBlock title="Hello {!$User.FirstName}!">
            You are viewing the {!account.name} account.
        </apex:pageBlock>
        <apex:pageBlock title="Contacts">
            <apex:pageBlockTable value="{!account.Contacts}" var="contact">
                <apex:column value="{!contact.Name}"/>
                <apex:column value="{!contact.Email}"/>
                <apex:column value="{!contact.Phone}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
        
        <apex:commandButton value="Export To Excel" Action="{!ExportToExcel}"/>  
    </apex:form>
</apex:page>

Remember to pass an Account ID as a query string to this page.

Controller:

public with sharing class ContactExportToExcel {
    
    Public List<String> selectedFieldsList {get;set;}
    Public List<Contact> conList {get;set;}
    
    public ContactExportToExcel(ApexPages.StandardController stdCtrl) {
        Account acc = (Account)stdCtrl.getRecord();
        conList = New List<Contact>();
        conList = [Select Id, Name, Email, Phone From Contact Where AccountId =: acc.Id];
    }
    
    Public PageReference ExportToExcel(){
        selectedFieldsList = New List<string>();
        selectedFieldsList.add('Name'); 
        selectedFieldsList.add('Email');
        selectedFieldsList.add('Phone');
        
        PageReference pgRef = New Pagereference('/apex'+'/ContactExportToExcel');
        pgRef.setRedirect(false); 
        return pgRef;
    }
    
}

Visualforce Page: ContactExportToExcel.vf

<apex:page standardController="Account" extensions="ContactExportToExcel" contentType="application/vnd.ms-excel#Contacts.xls" cache="true">
    <apex:pageBlock >
        <apex:pageblocktable value="{!conList }" var="tab">
            <apex:repeat value="{!selectedFieldsList}" var="field">
                <apex:column value="{!tab[field]}"/>
            </apex:repeat>
        </apex:pageblocktable>
    </apex:pageBlock>
</apex:page>

Output:

Select All Checkbox Using Apex in Visualforce Page

Controller:

public class Sample {
    
    public List<ContactWrapper> allContacts { get; set; }
    public Boolean allChecked { get; set; }
    
    public Sample() {
        allContacts = new List<ContactWrapper>();
        allChecked = false;
        
        for(Contact con: [SELECT Name, Title, Department, Email From Contact LIMIT 10]){ 
            allContacts.add(new ContactWrapper(con));
        } 
    }
    
    public PageReference CheckAll(){
        
        for(ContactWrapper con : allContacts){
            con.selected = allChecked;
        }
        
        return null;
    }
    
    public PageReference ProcessSelectedContacts(){
        
        for (ContactWrapper contactWrapper : allContacts ){
            if(contactWrapper.selected == true){
                /* Do Some Processing */
            }
        }
        
        return null;
    }
    
    public class ContactWrapper {
        
        public Contact con{get; set;}
        public Boolean selected {get; set;}
        
        public ContactWrapper(Contact c){
            con = c;
            selected = false;
        }
    }
}

Visualforce Page:

<apex:page controller="Sample">
    <apex:form>
        <apex:dataTable value="{!allContacts}" var="c" id="contactsTable">
            <apex:column >
                <apex:facet name="header">
                    <apex:inputCheckbox value="{!allChecked}">
                        <apex:actionSupport event="onclick" action="{!CheckAll}" rerender="contactsTable"/>
                    </apex:inputCheckbox>
                </apex:facet>
                <apex:inputCheckbox value="{!c.selected}"/>
            </apex:column>
            <apex:column value="{!c.con.Name}" headervalue="Full Name"/>
            <apex:column value="{!c.con.Title}" headervalue="Title"/>
            <apex:column value="{!c.con.Department}" headervalue="Department"/>
            <apex:column value="{!c.con.Email}" headervalue="Email"/>
        </apex:dataTable>
        <apex:commandButton action="{!ProcessSelectedContacts}" value="Process Selected Contacts"/>
    </apex:form>
</apex:page>

Output: