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 : System.Trigger.new) {
        
        //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()));
        }
        objNames.sort();
        return objNames;
    }
}

Visualforce Page:

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

Output:

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:form>
        <apex:outputPanel id="jspanel"> 
            <script>  
            function onCompleteMethod() {
                alert('{!message}')
            }
            </script>
        </apex:outputPanel>
        <apex:actionFunction name="afHelloWorld" action="{!HelloWorld}" rerender="jspanel"/>
        <apex:commandButton onclick="afHelloWorld();" oncomplete="onCompleteMethod()" value="Click me"/>
    </apex:form>
</apex:page>

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

<apex:page>
    <style>
        .vfHelpText a            {position:relative;}
        .vfHelpText a span       {display: none;}
        .vfHelpText a:hover span {display: block;
        position:absolute;
        top:1.25em;
        padding:2px 5px;
        left:-15em; width:15em;
        z-index:100;
        border:1px solid orange;
        background-color:#FEFDB9;
        color:black;
        }
        .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;
        }
    </style>
    
    <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-->
            <span>{!$ObjectType.Contact.fields.AccountId.inlineHelpText}</span>
        </apex:outputLink>
    </span>
</apex:page>