Tag Archives: Visualforce Page

Identify Salesforce User Experience Theme in Visualforce Page

We can use User.UITheme and User.UIThemeDisplayed in Visualforce Page to determine User Experience Theme.

User.UITheme : Returns the theme that is supposed to be used.
User.UIThemeDisplayed : Returns the theme that is actually being used.

User.UITheme and User.UIThemeDisplayed will return following values.

  • Theme1—Obsolete Salesforce theme
  • Theme2—Salesforce Classic 2005 user interface theme
  • Theme3—Salesforce Classic 2010 user interface theme
  • Theme4d—Modern “Lightning Experience” Salesforce theme
  • Theme4t—Salesforce mobile app theme
  • Theme4u—Lightning Console theme
  • PortalDefault—Salesforce Customer Portal theme
  • Webstore—Salesforce AppExchange theme

Sample Code:

<apex:page>
    <apex:pageBlock title="Theme">
        {!$User.UITheme}
        {!$User.UIThemeDisplayed}
    </apex:pageBlock>
</apex:page>

Visualforce Page Render As Advanced PDF

  • Advanced PDF renders Visualforce pages as PDF files with broader support for modern HTML standards, such as CSS3, JavaScript, and HTML5.
  • To use Advanced PDF, set renderAs="advanced_pdf" in the apex:page tag of a Visualforce page with API version 40.0 or later.
  • Advanced PDF supports in both Lightning Experience and Salesforce Classic.
  • It is similar to the existing process for rendering a Visualforce page as a standard PDF file.

Example:

<apex:page readOnly="true"
           standardController="Account"    
           applyHtmlTag="false"     
           sidebar="false"     
           showHeader="false"     
           cache="true"     
           renderAs="advanced_pdf"
           docType="html-5.0">
    <head>    
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />    
        <style type="text/css">
            @page {
            size: A4 landscape;    
            border: 1px solid black;    
            padding-left: 5px;    
            padding-right: 5px;      
            }
            th {  
            font-weight: bold;
            text-align: center;
            background-color: #92d5f0;
            color: black;
            padding: 8px;
            }
            td {    
            font-size: 15px;
            text-align: left;
            padding: 8px;
            }
            table{
            border-collapse: collapse;
            }
            table, th, td {
            border: 1px solid black;
            }
        </style>    
    </head>    
    <center>    
        <h3>{!Account.Name}</h3>    
    </center>    
    <table width="100%">    
        <tr>
            <th>Name</th>    
            <th>Phone</th>
            <th>Email</th> 
        </tr>    
        <apex:repeat value="{!Account.Contacts}" var="con">    
            <tr>                
                <td>{!con.Name}</td>    
                <td>{!con.Phone}</td>
                <td>{!con.Email}</td>    
            </tr>    
        </apex:repeat>    
    </table>    
</apex:page>

Output:

Google Charts in Visualforce Page

Salesforce has built in component for charts in visualforce page, but the limitation is some types of charts are not available. In such cases, we can use Google Chart for different types of charts. Here is an example of Google Chart in visualforce page.

  • You can find the list of all Google charts at Google Chart Gallery.
  • Find the right chart as per your requirement and go through the documentation for the data requirements of the chart.
  • You can download the Google Chart Javascript and upload it as a static resource, or you can direct call the Google Chart Javascript url in visualforce page.
  • Every Google Chart requires data in specific format, create the right forma data in apex controller, before sending it to visualforce page Google Chart.

Example: In below example I’m showing number of Accounts group by Country in a 3D Pie Chart.

Apex Class:

global with sharing class AccountChartController {
    
    @RemoteAction
    global static List<AggregateResult> getAccountData(){
        List<AggregateResult> accGroupList = [Select BillingCountry Country, Count(Id) NumberOfAccounts
                                              From Account Where BillingCountry != null
                                              Group By BillingCountry];
        return accGroupList;
    }
}

Visualforce Page:

<apex:page controller="AccountChartController">
    <!--Google Chart Javascript Resource-->
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"/>
    
    <!--Javascript Remote Function To Call Apex Controller Method-->
        <script type="text/javascript">
            //Load google chart
            google.charts.load('current', {packages: ['corechart']});
    google.charts.setOnLoadCallback(drawChart);
    
    var accData; //Variable to store data
    //Call remote action method
    AccountChartController.getAccountData(function(result, event){
        accData  = result; //get data from apex controller
    },{escape:true});
    
    //Draw google chart
    function drawChart() {
        //Create the data table.
        var data = new google.visualization.DataTable();
        //Add datatable columns
        data.addColumn('string', 'Country');
        data.addColumn('number', 'Number of Accounts');
        
        //Add datatable rows
        for(i = 0; i< accData.length; i++){
            data.addRow([accData[i].Country, accData[i].NumberOfAccounts]);
        }
        
        //Set chart options
        var options = {
            'title':'Accounts Group by Country',
            is3D: true,};
        
        //Instantiate and draw the chart.
        var chart = new google.visualization.PieChart(document.getElementById('myPieChart'));
        chart.draw(data, options);
    }
    </script>
    
    <!--Identify where the chart should be drawn-->
    <div id="myPieChart" style="width: 900px; height: 700px;"/>
</apex:page>

Output:

Invoking REST API From Visualforce Page

Usually we make Rest API calls from Salesforce to External systems to get the data or to pass the updates to External Systems, using Apex class. But sometimes we can have a situation, like where we need to make a call to external systems from the visual force page.

Salesforce has introduced a concept called AJAX ToolKit, which help us to make REST API call from the JavaScript in Visualforce Page. Here is an example to invoke REST API from Visualforce Page. In below example I’m using https://exchangeratesapi.io/ web service HTTP callout and send a GET request to get the foreign exchange rates. The foreign exchange rates service sends the response in JSON format.

Visualforce Page:

<apex:page>
    <apex:includeScript value="//code.jquery.com/jquery-1.11.1.min.js" />
    <script>
    function apiCall() {
        //Get a reference to jQuery that we can work with
        $j = jQuery.noConflict();
        //endpoint URL
        var weblink = 'https://api.exchangeratesapi.io/latest?base=USD'; 
        
        $j.ajax({
            url: weblink,
            type: 'GET', //Type POST or GET
            dataType: 'json',
            beforeSend: function(request) {
                //Add all API Headers here if any
                //request.setRequestHeader('Type','Value');
            },
            
            crossDomain: true,
            //If Successfully executed 
            success: function(result) {
                //Response will be stored in result variable in the form of Object.
                console.log('Response Result' + result);
                
                //Convert JSResponse Object to JSON response
                var jsonResp = JSON.stringify(result);
                document.getElementById("apiData").innerHTML = jsonResp;
            },
            
            //If any Error occured
            error: function(jqXHR, textStatus, errorThrown) {
                //alert('ErrorThrown: ' + errorThrown);
            }
        });
    }
    </script>
    <apex:form>
        <!--call javaScript-->
        <input type="button" value="Call API" onclick="apiCall()" />
        <div id="apiData">
        </div>
    </apex:form>
</apex:page>