When Salesforce Admin Should Code

Hi Friends, Hope you all are enjoying my posts. In this post, I will let you all especially Salesforce Admins when they should code, in other words, what are the scenarios when admin need to code.

I came up with some scenarios where admin should go for the code.

1 – Throw an Error message When trying to delete Account that has Closed Won Opportunity.

We can not show the Error message when trying to delete the account which has Closed Won Opportunity using any Salesforce  Out-of-Box functionality. So, admin should go for Apex Trigger to show the proper Error message.



Use below code for trigger to show the error

[codesyntax lang="java" container="div" doclinks="1"]
trigger AccountTrigger on Account (before delete) {
// Prevent the deletion of accounts if they have related won opportunities.
if(Trigger.isBefore && Trigger.isDelete){
for(Account acc : [Select Id, Name, (Select Id, Name From Opportunities WHere IsClosed=true) From Account
WHere Id IN : Trigger.oldMap.keySet()]){
if(acc.Opportunities.size() > 0){
trigger.oldMap.get(acc.Id).addError('Can not Delete Account, Because Account have one or more Closed Opportunity ');

4.png2 – Restoring the duplicate Data from the Recycle bin. A Salesforce admin should go for apex trigger in order to prevent the duplicate record in Salesforce Database

3 – Update Associated Contacts when a User is created or Update.

Another example where admin should code is the requirement to update the associated contact whenever a user is created or updated.


[codesyntax lang="java" container="div" doclinks="1"]
trigger UserTrigger on User (After insert, After Update) {
/* Take a set of Id to store the Contact Ids */
Set contactIdsSet = new Set();
/* prepare a Map with Contact Ids and User Name */
Map contactIdswithUserName_Map = new Map();
for(User u : Trigger.New){
if(u.ContactId != null){
contactIdswithUserName_Map.put(u.ContactId, U.Name);


/* Call handler Class method to update Contact with Respective User FirstName + LastName */


UserTriggerHandler – Use @future annonation to make the dml

[codesyntax lang="java" container="div" doclinks="1"]
public class UserTriggerHandler{

public static void updateContacts(Map contactIdswithUserName_Map){
List contactList = new List();
contactList = [Select Id, Name, UserName__c From Contact Where Id IN : contactIdswithUserName_Map.keySet()];
for(Contact c : contactList){
c.UserName__c = contactIdswithUserName_Map.get(c.Id);

update contactList;

4 – Writing the Rollups for the lookup Relationship

Rollup the child record data into parent record.

See the post here

5 – Automatic Lead Conversion

Convert Lead automatically when lead reaches the certain stage. like Qualified, or any other stage that you want.

To do this you can go for process builder which will call an apex class method and do the conversion


Create a trigger on Lead object and then call the handler class method

trigger code

[codesyntax lang="java" container="div" doclinks="1"]
trigger LeadTrigger on Lead (After Insert, After Update) {
List leadIdsSet = new List();
for(Lead l : Trigger.New){
if(l.status == 'Qualified')


Handler Class Code

[codesyntax lang="java" container="div" doclinks="1"]
Public class LeadTriggerHandler
public static void convertLeads(List LeadIds)
/* fetch the converted Status */
LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
List LeadconvertList = new List();
for(id currentlead: LeadIds){
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion

if (!LeadconvertList.isEmpty()) {
List lcr = Database.convertLead(LeadconvertList);

Whenever I will found more scenarios then I will update this post!

Any questions comment below enjoy learning enjoy salesforce.

Sharing is caring 🙂



Our Score
Click to rate this post!
[Total: 0 Average: 0]

Leave a Reply