Skip to main content

I have written an apex code to create task notification on Lead when an event is created with subject line "Something" and the Lead owner role with particular role. 

After converting the lead to account, then the account owner is getting email task notifications even though that owner doen't have the role.

I cheched the Lead settings too.

And while converting the lead to account, assigning to new owner which will be on account.

2 answers
  1. May 6, 2022, 4:45 PM

    Hi Priya, here is the code

    TriggerHandler:

    public class EventTriggerHandler extends TriggerHandler{

        public static EventHelper helper;    

        public static List<Task> tskList = new List<Task>(); 

        public static Map<Id, Lead> mapLead = new Map<Id, Lead>(); 

            

        public EventTriggerHandler(){

            helper = new EventHelper();

        }

        

        public override void afterInsert(){        

            Set<Id> leadIds = Utils.pluckFieldIds(Trigger.new, 'WhoId');

            Map<Id, sObject> mapEvent = Utils.mapIdToRecord(Trigger.new, 'WhoId');

            mapLead = LeadManager.getMDRLeadsById(leadIds);   

            for(Lead ld : LeadManager.getMDRLeadsById(leadIds).values())

            {

                Event leadEvt = (Event)mapEvent.get(ld.Id);             

                helper.calendlyEventNotification(leadEvt);

            }        

            helper.upsertRecords(tskList);              

        }    

    }

    Helper Class:

    public void calendlyEventNotification(Event eve){

            try{

                Id rtId = TaskHelper.getRTId('Member');

                if(eve.Subject.contains('Event Notification') &&

                  (EventTriggerHandler.mapLead != null && EventTriggerHandler.mapLead.containsKey(eve.WhoId))){                  

                    Task tsk = new Task();

                        tsk.Subject = 'Event Notification';

                        tsk.Description= EventTriggerHandler.mapLead.get(eve.WhoId).Name +' has scheduled an interview through Calendly. Please go ahead and convert the lead;  

                         tsk.RecordTypeId = rtId;

                        tsk.Priority = 'High';

                        tsk.OwnerId = EventTriggerHandler.mapLead.get(eve.WhoId).ownerId;                      

                        tsk.WhoId = EventTriggerHandler.mapLead.get(eve.WhoId).Id;                  

                        tsk.IsReminderSet = true;

                        tsk.ReminderDateTime = System.now();

                        tsk.ActivityDate = system.today();

                        EventTriggerHandler.tskList.add(tsk);                   

                }            

            } catch (Exception e ){

                Utils.debugException(e);

            }

        }

    public void upsertRecords(List<Task> taskList){

            if(!taskList.isEmpty()){

                Utils.upsertRecords(EventTriggerHandler.tskList);             

            }

        }    
0/9000