Category Archives: Salesforce

Salesforce SOSL Example

  • SOSL (Salesforce Object Search Language) is a search language in Salesforce, we can search in multiple objects at same time using SOSL. In SOQL, we can query only one object at a time but in SOSL, We can search for some specified string in multiple objects at the same time.
  • SOSL query begins with the required FIND clause.
  • The search string should be at least two characters long.
  • SOSL is used if we don’t know in which object the data is present.
  • We can mention in which fields of all the sObjects,we want to search for the string specified. Suppose you have to performed search on two objects Account & Contact. Then you can mention like, for list returned with Account results only (Name, Industry) fields should be returned, and for Contacts results (firstName, lastName) should be returned.
  • We can retrieve multiple objects and field values efficiently when the objects may or may not be related to each other.
  • We can query only on fields whose data type is text, phone and Email.
  • The result of SOSL is a list of lists of sObjects.
    The returned result contains the list of sObjects in the same order as order mentioned in SOSL query.
  • If a SOSL query does not return any records for a specified sObject type, then search results include an empty List for that sObject.
  • We can use SOSL in classes but not in Triggers.
  • We cannot perform DML operation on search result of SOSL.

Here in below example, there is a input text box and a command button which will search for the entered string in two Objects Accounts and Contacts and returned result will be shown in the page block tables.

Visualforce Page:

<apex:page controller="SampleController">
    <apex:form>
        <apex:inputText value="{!searchStr}"/>
        <apex:commandButton value="Get Records Using SOSL" action="{!Search}"/>
        <br/>
        <br/>
        <apex:pageBlock title="Accounts">
            <apex:pageblockTable value="{!accList }" var="acc">
                <apex:column value="{!acc.name}"/>
                <apex:column value="{!acc.Type}"/>
            </apex:pageblockTable>
        </apex:pageBlock>
        <apex:pageBlock title="Contacts">
            <apex:pageblockTable value="{!conList}" var="con">
                <apex:column value="{!con.name}"/>
                <apex:column value="{!con.email}"/>
            </apex:pageblockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Controller:

public class SampleController {
    Public List<Contact> conList {get; set;}
    Public List<Account> accList {get; set;}
    Public String searchStr {get; set;}
    
    Public void Search(){
        conList = New List<contact>();
        accList = New List<account>();
        List<List <sObject>> searchList = [FIND :searchStr IN ALL FIELDS RETURNING  Account (Id,Name,Type), Contact(Name,Email)];
        accList = ((List<account>)searchList[0]);
        conList  = ((List<contact>)searchList[1]);
    }
}

Output:

Types of Reports in Salesforce

Format Description
Tabular Tabular reports are the simplest and fastest way to look at data. Similar to a spreadsheet, they consist simply of an ordered set of fields in columns, with each matching record listed in a row. Tabular reports are best for creating lists of records or a list with a single grand total. They can’t be used to create groups of data or harts, and can’t be used in dashboards unless rows are limited. Examples include contact mailing lists and activity reports.
Summary Summary reports are similar to tabular reports, but also allow users to group rows of data, view subtotals, and create charts. They can be used as the source report for dashboard components. Use this type for a report to show subtotals based on the value of a particular field or when you want to create a hierarchical list, such as all opportunities for your team, subtotaled by Stage and Owner. Summary reports with no groupings show as tabular reports on the report run page.
Matrix Matrix reports are similar to summary reports but allow you to group and summarize data by both rows and columns. They can be used as the source report for dashboard components. Use this type for comparing related totals, especially if you have large amounts of data to summarize and you need to compare values in several different fields, or you want to look at data by date and by product, person, or geography. Matrix reports without at least one row and one column grouping show as summary reports on the report run page.
Joined Joined reports let you create multiple report blocks that provide different views of your data. Each block acts like a “sub-report,” with its own fields, columns, sorting, and filtering. A joined report can even contain data
from different report types.

Relationships Object Records in Visualforce Page

We can have a requirement to show master object record and the respective child records in a Visualforce Page. So, for that we don’t have to write code to retrieve child object record. We can show the child records without query it.

Here is an example I’m rendering a Visualforce Page as “pdf” with Account information and the respective contact records information.

Visualforce Page:

<apex:page standardController="Account" showHeader="false" renderAs="pdf">
    
    <b>Name:</b> {!Account.Name}<br/>
    <b>Account Number:</b> {!Account.AccountNumber}<br/>
    <b>Type:</b> {!Account.Type}<br/>
    <b>Industry:</b> {!Account.Industry}<br/>
    <b>Phone:</b> {!Account.Phone}<br/>
    <p/>
    
    <table width="100%" cellpadding="2" cellspacing="2">
        <tr>
            <td><b>First Name</b></td>
            <td><b>Last Name</b></td>
            <td><b>Email</b></td>
            <td><b>Phone</b></td>
        </tr>
        <apex:repeat value="{!Account.Contacts}" var="con">
            <tr>
                <td>{!con.FirstName}</td>
                <td>{!con.LastName}</td>
                <td>{!con.Email}</td>
                <td>{!con.Phone}</td>
            </tr>
        </apex:repeat>
    </table>
</apex:page>  

SOQL Query for Tasks, Notes and Events by Object Type

Tasks:

//The below query returns a list of Tasks related to Leads
SELECT Id, Who.Id, Who.Type FROM Task WHERE Who.Type = 'Lead'

Notes:

//The below query returns a list of Notes where the parent is a specific type, in this case Opportunity
SELECT Id, Parent.Id, Parent.Type FROM Note WHERE Parent.Type = 'Opportunity'

Events:

//The below query returns a list of Events related to Account and Opportunity
SELECT Id, Subject, What.Type, whatId FROM Event WHERE What.Type IN ('Account', 'Opportunity')

Invoke Apex Class from Trigger

You can create static or instance methods on your Apex Class. In your trigger you can then have code that calls these classes. This is very similar to the way method calling works in other languages. Here is a simple example.

Apex Class:

public class SampleClass
{
    public void SampleMethod(List<Account> listAccount, Map<Id, Account> mapAccount){
        
    }
}

Apex Trigger:

trigger SampleAccount on Account (before insert) 
{
    for(Account a : trigger.New){
        SampleClass obj = new SampleClass(Trigger.New, Trigger.NewMap);
        obj.SampleMethod();
    }
}