Tag Archives: Apex

Set Up Governor Limit Warning Email in Salesforce

  • Log in to Salesforce as an administrator User.
  • Click Setup || Administration Setup || Manage Users || Users
  • Click Edit next to the name of the user to receive the email notifications.
  • Select the Send Apex Warning Emails option.
  • Click Save.

Note: You can specify users in your organization to receive an email notification when they invoke Apex code that surpasses 50% of allocated governor limits.

Comparable: Sorting Objects in Salesforce

The Comparable interface adds sorting support for Lists that contain non-primitive types, that is, Lists of user-defined types.

To add List sorting support for your Apex class, you must implement the Comparable interface with its compareTo method in your class.

When a class implements the Comparable interface it has to implement a method called compareTo(). This method returns an Integer value that is the result of the comparison. In that method the code has to be written to decide if two objects match or if one is larger than the other.

The implementation of this method should return the following values:

  • 0 if this instance and objectToCompareTo are equal
  • > 0 if this instance is greater than objectToCompareTo
  • < 0 if this instance is less than objectToCompareTo

Let’s take a look at a simple class that takes in an Employee object, stores the employee’s data in it and allows us to sort a list of Employees by their Employee Id.

Apex Class:

public class Employee implements Comparable {

    public Integer Id;
    public String Name;
    public Decimal Salary;

    public Employee(Integer i, String n, Decimal s) {
        Id = i;
        Name = n;
        Salary = s;

    public Integer compareTo(Object objToCompare) {
        Employee emp = (Employee)objToCompare;
        if (Id == emp.Id){
            return 0;
        else if (Id > emp.Id){
            return 1;
            return -1;        

Execute the below code in Developer Console.

List<Employee> empList = new List<Employee>();
empList.add(new Biswajeet.Employee(104,'John Doe', 5000.00));
empList.add(new Biswajeet.Employee(102,'Joe Smith', 9000.00));
empList.add(new Biswajeet.Employee(103,'Caragh Smith', 10000.00));
empList.add(new Biswajeet.Employee(101,'Mario Ruiz', 12000.00));

//Sort using the custom compareTo() method

//Write list contents to the debug log
for (Employee e : empList){


Retrieve the Record Type Which are Accessible by User’s Profile only

The below code will add only the record type of “Contact” object accessible to the user.

List <SelectOption> rtList = new List <SelectOption>();
for (RecordTypeInfo rtInfo: Contact.SObjectType.getDescribe().getRecordTypeInfos() ) {
	if(rtInfo.isAvailable()) {
		rtList.add(new SelectOption(rtInfo.getRecordTypeId(), rtInfo.getName()));

Partial Apex DML Example in Salesforce

List <Account> conList = new List <Account> {
 new Account(Name = 'Test Account1', Phone = '8888888888', Industry = 'Agriculture'),
 new Account(Name = 'Test Account2', Phone = '7777777777', Industry = 'Banking'),
 new Account(Name = 'Test Account3', Phone = '9999999999', Industry = 'Finance'),
 new Account()

Database.SaveResult[] srList = Database.insert(conList, false);

for (Database.SaveResult sr : srList) {
 if (sr.isSuccess() ) {
  //Develop code for successfully inserted Accounts
  System.debug('Successfully inserted Account ' + sr.getId());
 } else {
  for (Database.Error err : sr.getErrors()) {
   //Develop code for failed Accounts
   System.debug(err.getStatusCode() + ' : ' + err.getMessage() + ' : ' + err.getFields());