When Salesforce Admin should Code

When Salesforce Admin should Code

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.

1.png

2.png

Use below code for trigger to show the error

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.

UserTrigger

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.updateContacts(contactIdswithUserName_Map);
}

 

UserTriggerHandler – Use @future annonation to make the dml

public class UserTriggerHandler{

@future
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

OR

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

trigger code

trigger LeadTrigger on Lead (After Insert, After Update) {
List leadIdsSet = new List();
for(Lead l : Trigger.New){
if(l.status == 'Qualified')
leadIdsSet.add(l.id);
}

LeadTriggerHandler.convertLeads(leadIdsSet);
}

Handler Class Code

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.setLeadId(currentlead);
Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion
LeadconvertList.add(Leadconvert);
}

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 🙂

 

 

AMIT SINGH

My name is Amit Singh. I am a Certified Advanced Salesforce Developer. I am 4X salesforce certified professional @PureSoftware Ltd. I love to solve the queries in Salesforce Success Community and Developer forum. I am in love with Salesforce because of its continuous improvement. Inject trailhead into your blood and you will be a salesforce champion

leave a comment

Create Account



Log In Your Account