Wednesday, December 11, 2013

Before Delete Trigger in Salesforce

Scenario:
    
  Write a Trigger on Account Object, All it does is to prevent Delete of an Account if a particular field(i.e.,Client_ID__c) not Null.

Trigger Code:

  trigger CannotDeleteAccountWithClientID on Account (before delete) 
 { 
  if(System.Trigger.IsDelete)
   { 
    for (Account Accts : trigger.old) 
    {
      if (Accts.Client_ID__c != null)
 Accts.addError('You cannot delete an Account Please contact your Salesforce.com Administrator for assistance.');
        }
    } 
   }
 }

Notes:

Trigger.oldReturns a list of the old versions of the sObject records.Note that this sObject list is only available in update and delete triggers.
Trigger.isDeleteReturns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.

No comments: