Restrict Lead Deletion in Progress

This trigger prevents users from deleting Leads with a Status of “Working-Contacted,” ensuring that active leads are not accidentally removed.

Practice similar scenarios using this YouTube playlist.

Solution:
public class LeadTriggerHandler {
public static void handleActivitiesBeforeDelete (List<Task> leadRecords) {
                    for (Lead leadRec : leadRecords) {
                                System.debug(‘Found a lead’);
                                System.debug(leadRec.Status);
                       if (leadRec.Status == ‘Working-Contacted’) {
                                    System.debug(‘Yes, it is in progress’);
                                   leadRec.addError(‘You cannot delete a Lead in progress’);
                     }
        }
}
public static void handle ActivitiesAfterInsert (List leadRecords) {
                             List taskListToInsert = new List ();
                                   for (Lead leadRec : leadRecords) {
                                       Task taskRecord = new Task();
                                       taskRecord.Priority = ‘High’;
                                       taskRecord.OwnerId = leadRec.OwnerId;
                                       taskRecord.Description = ‘Follow up with the new lead’;
                                       taskRecord.Status = ‘Not Started’;
                                       taskRecord.Subject = ‘Follow up’;
                                       taskRecord.WhoId = leadRec.Id;
                                       taskListToInsert.add(taskRecord);
              }
                     if(!taskListToInsert.isempty()){
                            insert taskListToInsert;
 }

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)