Tag Archives: Trigger

Restrict multiple approval for a single user to a single record in Salesforce

I had a requirement to restrict multiple approval for a single user to a single record. There were multiple steps of approval process and the approver may be in different queue or as a manager for a user.

So, I implemented it using a trigger. In this article I’ll demonstrate how to implement it.
Here Application__c is the object, on which 3 steps approval process has implemented.
A custom check box field Check_Approver__c has set value to true, in all 3 steps field update action.

In below trigger I filtered the ProcessInstanceStep object records with current user Id in ActorId and the object Id (here object Id means record Id) in ProcessInstance.TargetObjectId.

  • If an User will approve or reject a record, then the SOQL query will return a single record.
  • If the SOQL query return a record, then trigger will throw an error message.

Trigger source code:

trigger ApplicationTrigger on Application__c (before Update)
{
    if(trigger.isUpdate){

        Id currentUserId = UserInfo.getUserId();
        for(Application__c sf: trigger.new){   
                  
            if(sf.Check_Approver__c == true){
              
                List<processinstancestep> existingApprovals = [SELECT ActorId
                                    FROM ProcessInstanceStep WHERE ProcessInstance.TargetObjectId = :sf.Id
                                    AND (StepStatus = 'Approved' OR StepStatus = 'Rejected')
                                    AND ActorId = :currentUserId];
                                      
                if(existingApprovals != null){
                  
                    if(existingApprovals.size() > 0){

                        sf.addError('You have already approved or rejeted the record.');
                    }
                }                  
             }
         }                         
    }
}

Difference between trigger and workflow rule

Workflow Rules:

  • Workflow is an automated process that fired after an action, based on evaluation criteria and rule criteria.
  • Workflow actions are Field Update, Email alert, Task alert and outbound message.
  • Workflow will be helpful to update the same object or master object in custom master-detail relationships.
  • We cannot fire workflows after record has been deleted.
  • We cannot query from database on workflow.

Trigger:

  • Trigger is a piece of code that executes before or after, when an DML event occurs like insert, update or Delete.
  • Trigger executes before or after these types of operations insert, update, delete, merge, upsert & undelete.
  • We can access the trigger across the object and related to that objects.
  • We can use DML operations in one trigger.
  • We can use SOQL’s from data base in one trigger.

Validation rule in apex trigger

Salesforce provides validation rules in configuration for standard and custom objects. sometimes the requirements may not be fulfilled using validation rule, especially when the validation criteria is very complex or need querying in database to check previously created data. In such a case we can write validation logic in trigger.

In this article, I will demonstrate how to write validation logic in apex trigger.

Here in below example, there is before insert and before update trigger on Opportunity object. In this trigger the amount field has validated, for less than 5000.

trigger TriggerOpportunity on Opportunity (before insert, before update){
    for(Opportunity opp:trigger.new){
        if(opp.Amount < 5000){
            opp.Amount.adderror('Amount cannot be less than 5000.');
        }
    }
}

download