Category Archives: Salesforce

How to show more records in related list in Salesforce?

When viewing a record detail page (e.g. Contact detail) the related lists (e.g. Cases related list) shows a limited number of related records at a time.

download (1)

So, if you have more than 5 (by default) Cases tagged to an Contact, in the Contact detail page, you will see Show m more » | Go to list (n+) ». By default a will be 5 and b is total number of the records.

You can scroll down to the bottom of page, and look for Always show me more records per related list or Always show me fewer / more records per related list if you have click more before (even in previous login).
Always show me more records:

download (2)

Always show me fewer / more records:

download (3)

Clicking more or fewer increases and decreases the default number 5 (by default) of related list records displayed for all object record detail pages for the logged in user. If you notice, when you click more or fewer link, in the URL, Salesforce will add parameters rowsperlist=m (e.g. https://ap1.salesforce.com/0039000000ojedc?rowsperlist=10), this parameter will show related list up to 10 records for all related list.
Note: This change will be permanent for the logged in user for all tab, and can increase the related list records up-to 100.

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:

Get Map Result From SOQL Query in Salesforce

//Creating Map with account id as key and account record as value
Map<Id,Account> accountMap = new Map<Id,Account>([SELECT Id, Name, Site, Rating, AccountNumber From Account LIMIT 100]);
 
//Getting list of account using map.values method
List<account> accList = accountMap.values();
 
//Getting set of account Id's using map.keySet method
Set<Id> accIdSet = accountMap.keySet();
 
//Getting list of account Id's using list.addAll method
List<Id> accIdList.addAll(accIdSet);

Salesforce Like Operator in SOQL Query

Like Operator in SOQL act as a Comparison operator for String Field Expression. Expression is true if the value in the specified fieldName matches the characters of the text string in the specified value. The LIKE operator in SOQL and SOSL is similar to the LIKE operator in SQL; it provides a mechanism for matching partial text strings and includes support for wildcards.

Use Cases Of LIKE Operator:

  • The LIKE operator is supported for string fields only.
  • The % and _ wildcards are supported for the LIKE operator.
  • The % wildcard matches zero or more characters.
  • The _ wildcard matches exactly one character.
  • The text string in the specified value must be enclosed in single quotes.
  • The LIKE operator is supported for string fields only.
  • The LIKE operator performs a case-insensitive match, unlike the case-sensitive matching in SQL.
  • The LIKE operator in SOQL and SOSL supports escaping of special characters % or _.
  • Don’t use the backslash character in a search except to escape a special character.

Example1:
The following query matches Appleton, Apple, and Appl, but not Bappl:

SELECT Id, Name
FROM Account
WHERE Name LIKE 'appl%'

Example2:
The following query matches Appleton, Apple, and Bappl, but not Appl:

SELECT Id, Name
FROM Account
WHERE Name LIKE 'appl_%'