Category Archives: Salesforce

Avoid Duplicate Record Using Apex Trigger

Here in below example the trigger is on “Account” object, to check duplicate Account Names.

Apex Trigger:

trigger accountDuplicateCheck on Account (before Insert, before Update) {
    Map<String, Account> accMap = new Map<String, Account>();
    for (Account acc : {
        //Make sure we don't treat account Name that isn't changing during an update as a duplicate.  
        if (System.Trigger.isInsert || (acc.Name != System.Trigger.oldMap.get(acc.Id).Name)) {
            //Make sure another new account isn't also a duplicate  
            if (accMap.containsKey(acc.Name)) {
                acc.Name.addError('Another account has the ' + acc.Name + ' same name.');
            } else {
                accMap.put(acc.Name, acc);
    //Query to find all the Accounts in the database that have the same name as any of the Accounts being inserted or updated.  
    for (Account acc : [SELECT Name FROM Account
                        WHERE Name IN :accMap.KeySet()]) {
                            Account newAcc = accMap.get(acc.Name);
                            newAcc.Name.addError('An account with this name ' + acc.Name + ' already exists.');

Get All Objects in Salesforce Org

We can use Schema.getGlobalDescribe() to get all properties of sObject.
Apex Class:

public class SampleController {
    Public string selectedObj{get;set;}
    Public List<Selectoption> getObjList(){
        List<Schema.SObjectType> objList = Schema.getGlobalDescribe().Values();     
        List<SelectOption> objNames = new List<SelectOption>();
        objNames.add(new SelectOption('','-- Select --'));
        for(Schema.SObjectType obj : objList)
            objNames.add(new SelectOption(obj.getDescribe().getName(),obj.getDescribe().getLabel()));
        return objNames;

Visualforce Page:

<apex:page controller="SampleController">
    <apex:form >
            <apex:outputlabel value="Object Name" for="plObj"/>      
            <apex:selectList value="{!selectedObj}" multiselect="false" id="plObj" size="1">
                <apex:selectOptions value="{!ObjList}"/>


Passing Variables from Apex to JavaScript

Sometimes we need to pass variables from apex to javascript. Here is an example to pass variables from apex to javascript. In below example I’m invoking one apex method using action function. In the apex method I’m defining the apex variable value. On complete of action function I’m invoking one javascript method and using the apex variable in the javascript method.

Visualforce Page:

<apex:page controller="SampleController">  
        <apex:outputPanel id="jspanel"> 
            function onCompleteMethod() {
        <apex:actionFunction name="afHelloWorld" action="{!HelloWorld}" rerender="jspanel"/>
        <apex:commandButton onclick="afHelloWorld();" oncomplete="onCompleteMethod()" value="Click me"/>

Apex Class:

public class SampleController {
    public String message {get;set;}
    public pageReference HelloWorld() {
        message = 'Hello World!!';
        return null;

Display Standard Salesforce Help Text on Visualforce Page

        .vfHelpText a            {position:relative;}
        .vfHelpText a span       {display: none;}
        .vfHelpText a:hover span {display: block;
        padding:2px 5px;
        left:-15em; width:15em;
        border:1px solid orange;
        .helpOrb {
        background-image: url(/img/help/helpOrbs.gif);
        background-position: 0 0;
        width: 20px;
        height: 15px;
        .helpButtonon .helpOrb {
        background-position: top right;
        .helpButton .helpOrb {
        background-position: top left;
    <span class="vfHelpText">
        <apex:outputLink value="javascript:return false;">
            <img src="/s.gif" alt="" class="helpOrb" />
            <!--s.gif image is standard image no need to upload anywhere-->