What is apex trigger in salesforce?

Apex trigger is block of code wrapped by {} and fire whenever object(table) records are (insert,update and delete) all DML Operation like.

Suppose you want to set Title field of Contact object on the basis of “Gender__c” and “Marital_Status__c” custom field. There are no standard Gender and Marital Status fields for Contact Object in Salesforce. So please add both custom fields Gender and Marital Status in Contact Object if you want to run following apex trigger.

If you want to create custom fields in contact object

Step-1: goto to top-right of screen and click setup link and type Contact in search box after that click highlighted fields link.

Step-2:  Goto Contact Custom Fields & Relationships section and click on new button to add custom field. in next screen select a data type Picklist not multi select and click next button.

Step-3: Filled all required fieldsFor option value check “Enter values, with each value separated by a new line” add two option Male and Female separated by new line.

Step-4: Click Next button.

Step-5: Click Save button.

Marital Status custom field you can create in same way.

Now Let’s create trigger on Contact Object to set Title fields of contact.
Apex TriggerApex Trigger

trigger SetTitleTrigger on Contact (before insert,After Update) {
    List conatctList = new List();    
        for(Contact cont : Trigger.New){
                if(String.isNotBlank(cont.Gender__c) && (cont.Gender__c.toLowerCase()=='female'))
                    if(String.isNotBlank(cont.Marital_Status__c) && (cont.Marital_Status__c.toLowerCase()=='single'))
                        cont.Title = 'Ms.';
                        cont.Title = 'Mrs.';
                     cont.Title = 'Mr.';   

Apex Trigger Context Variables

List of context variables are available in apex trigger. Some of variables are very useful i.e., isInsert, isUpdate, isDelete, isBefore and isAfter. I suggest that you should create only one trigger for each object if required. Example

trigger SetTitleTrigger on Contact(before insert,before update,before delete,after update, after delete) {
    List conatctList = new List();    
	// execute (in both case before and after) if insert a contact
     if(Trigger.isInsert && Trigger.isBefore){
	// execute before insert a contact
	// you can set some field value before inserting record
     if(Trigger.isInsert && Trigger.isAfter){
	// execute after insert a contact
	//Here you can get 

In same way you can check isUpdate and isDelete along with isBefore and isAfter context variables.

Difference Trigger.new and Trigger.newMap

Trigger.new is a apex context variable that return list of sObject while Trigger.newMap return Map of Id and sObject.

List objList = Trigger.new;
Map<Id,sObject> objList = Trigger.newMap;

There are also two variables Trigger.old and Trigger.OldMap. These two variable return list and map of sObject if record is update or delete only.

How to get selected option value using jquery?

To get selected option value of select HTML tag use option:selected property.
<select id=”selectid”>

<option value=’1′>One</option>

<option value=’2′>Two</option>

<option value=’3′>Three</option>

<option value=’4′>Four</option>



$(‘#selectid option:selected’).attr(‘value’);


How to find radio button selection using jquery?

/*****************************************STYLE SHEET****************************************/

.border-right{border-right:1px solid #EEE; background-color:FFF; cursor:pointer;}
.border-right:hover{background-color:#e7f1fa !important}
.hide-radio{ display:none;}
.selected-radio{background-color:#e7f1fa; font-weight:bold; }


<table style=”font-size:12px;”>
<td class=”border-right”><input type=”radio” name=”time” value=”8hours” checked=”checked”/>8 hours</td>
<td class=”border-right”><input type=”radio” name=”time” value=”24hours”  class=”hide-radio”/>24 hours</td>
<td class=”border-right”><input type=”radio” name=”time” value=”48hours” class=”hide-radio”/>48 hours</td>
<td class=”border-right”><input type=”radio” name=”time” value=”3days” class=”hide-radio”/> 3 days</td>
<td class=”border-right”><input type=”radio” name=”time” value=”5days” class=”hide-radio”/> 5 days<br /></td>
<td class=”border-right”><input type=”radio” name=”time” value=”7days” class=”hide-radio”/>7 days</td>
<td class=”border-right”><input type=”radio” name=”time” value=”14days” class=”hide-radio”/>14 days<br /></td>
<td class=”border-right”><input type=”radio” name=”time” value=”1month” class=”hide-radio”/>1 Month<br /></td>






How find which radio button is selected using jquery


<input type=”radio” id=”ratingPoint4″ name=”ratingPoint” value=”1″ />

<input type=”radio” id=”ratingPoint4″ name=”ratingPoint” value=”2″ />

<input type=”radio” id=”ratingPoint4″ name=”ratingPoint” value=”3″ />

<input type=”radio” id=”ratingPoint4″ name=”ratingPoint” value=”4″ />

<input id=”survey-submit” type=”submit” class=”survey-button aui-button” value=”Post”/>



Compare two array in javascript

var cubes = [ [1, 2, 3], [4, 5, 6], [7, 8, 9],];

var cubes2 = [ [10, 2, 3], [4, 5, 6], [7, 8, 9],];

var isIdentical=true;
for(var i = 0; i < cubes.length; i++) {
    var cube1 = cubes[i];
    var cube2 = cubes2[i];
    for(var j = 0; j < cube1.length; j++) {
          alert('Both matrices are not equal'+"cube1[" + i + "][" + j + "] = " + cube1[j]+'!=='+"cube2[" + i + "][" + j + "] = " + cube2[j]);
        } else{
         //console.log("cube1i + "][" + j + "] = " + cube1[j]);

 alert('Both matrices are equal');

What is object in salesforce?

Objects in Salesforce is just like a database table. Objects support all the functionality you expect in a table, with additional enhancements that make them more powerful and versatile. Salesforce use object to store data in database.

Whenever we create a custon object, Salesforce create an apex class using suffix “__c”, suppose your object name “Employee” Salesforce create automatically a class named “Employee__c”. You can use this class to access all fields of “Employee” Object(Table).

There are two type of object:

  1. Standard Object
  2. Custom (User defined) object.

All Data types supported by salesforce

Prevent Recursive Trigger in Salesforce Apex

What is recursive trigger?

You have learned about recursion process of loop. Same logic here means a trigger fire by itself. In detail you say that recursive trigger fire by some code of block which is written in same apex trigger or INSERT or UPDATE.

public Class AvoidRecursive
    private static boolean isRun = true;
    public static boolean runOnce()
            return true;
           return isRun;

Use above runOnce() static method in trigger before insert/update statement like

     // Database insert/update/delete statement