0

I have a trigger that on contact creation/insertion, I need to get its ID and make a new task associated with that ID.

However, I have System.debugged Trigger.new, and I see that the newly created contact is not in Trigger.new.

My code is here:

trigger CreateTaskforEvents on Contact (after update, after insert) {

    if(RecursiveTrigger_LOContactRatingFU.isFirstTime){
        RecursiveTrigger_LOContactRatingFU.isFirstTime = false;      
    List<Task> insertEvents = new List<Task>();   
    List<Task> updateEvents = new List<Task>(); 
    List<Task> taskList = new List<Task>();
    List<Contact> triggerList = Trigger.new;

    Boolean regFlag = false;

    Set<Id> allContacts = new Set<Id>();

    Integer count = 0;


    //Step 1 Create a set of all Contacts Ids to Query   
    for (Contact c: triggerList){
        allContacts.add(c.id);
        System.debug(c.id)
    }



    Map<Id, List<Task>> taskMap = new Map<Id, List<Task>>();

    //GEt List of all tasks
   for (Task t : [SELECT Id, whoId, Subject FROM Task WHERE whoId In : allContacts])
   {


       if(taskMap.containsKey(t.WhoId))
       {
          // taskMap.put(t.WhoId,new List<Task>());
           taskMap.get(t.WhoId).add(t);


       }else
       {
           taskMap.put(t.WhoId,new List<Task>{t});
       }

   }
    /**
     * trigger.new does not contain inserted new contact
     **/
    for (Contact c:triggerList){
        //If Phone Burner Event List Registered Flag is Checked
        if (c.Phone_Burner_Event_Reg__c == True && c.Phone_Burner_Event_List_Attended_Flag__c == False)
        {

            //Create Registerd Task
            Task eventRegTask = new Task();

            eventRegTask.ActivityDate = Date.today();
            eventRegTask.Priority = 'Normal';
            eventRegTask.Status = 'Not Started';
            eventRegTask.WhoId = c.Id;
            eventRegTask.OwnerId = c.Owner_ID__c;
            eventRegTask.Subject = 'Registered - ' + c.Phone_Burner_Events_Task_Subject__c;

            insertEvents.add(eventRegTask);
            System.debug(insertEvents);

        } else if (c.Phone_Burner_Event_List_Attended_Flag__c == True)
        {

            //loop through all tasks if there is already a registered task switch to attended else create new attended task
            //Get all tasks for contact and put in list


            for(Task t1 : taskMap.get(c.Id))
            {

                if (t1.Subject == 'Registered - ' + c.Phone_Burner_Events_Task_Subject__c)
                {

                    regFlag = True;

                    //Flip Registered to Attended
                    t1.Subject = 'Attended - ' + c.Phone_Burner_Events_Task_Subject__c;


                    updateEvents.add(t1);
                    //update t1;

                }
            }


            //If no registerd event yet create attended event
            if (!regFlag)
            {
                //Create Registerd Task
                Task eventAttTask = new Task();

                eventAttTask.ActivityDate = Date.today();
                eventAttTask.Priority = 'Normal';
                eventAttTask.Status = 'Not Started';
                eventAttTask.WhoId = c.Id;
                eventAttTask.OwnerId = c.Owner_ID__c;
                eventAttTask.Subject = 'Attended - ' + c.Phone_Burner_Events_Task_Subject__c;


                insertEvents.add(eventAttTask);
                //insert eventAttTask;


            }


        }

    }//end for Contact c:Trigger.net

        insert insertEvents;
        update updateEvents;

    }
}//end Trigger

How do I get the ID of the newly created contact, so that I can make a task right after insertion?

  • What did you see when you ran System.debug(Trigger.new)? If you are in an after insert event for a new Contact, that Contact will be included in that list. – David Reed Jul 3 at 14:54
  • So on System.debug(Trigger.new), I see one ID; however, it is not the ID of the newly created contact. It ends up being the contact linked to the newly created contact through a required field 'Referred By'. On Contact creation, I have to link an existing contact to it in the 'Referred By' Field. – Sarah Jul 3 at 15:01
  • I would double check that the id is the referenced id. Also In the code you provided I'm not seeing the debug statement your referring to, I am seeing the debug on insertEvents though – Zach Hutchins Jul 3 at 15:06
  • Sorry, I just edited it to show where I put it – Sarah Jul 3 at 15:11
  • N.B. allContacts is not required as it is the same as Trigger.newMap.keySet(); triggerList is also not required as you can just use Trigger.new; c.Phone_Burner_Event_Reg__c == True && c.Phone_Burner_Event_List_Attended_Flag__c == False is better coded as c.Phone_Burner_Event_Reg__c && !c.Phone_Burner_Event_List_Attended_Flag__c – cropredy Jul 3 at 17:46
1

I'm pretty sure your comment about the debugging process reveals the issue here:

So on System.debug(Trigger.new), I see one ID; however, it is not the ID of the newly created contact. It ends up being the contact linked to the newly created contact through a required field 'Referred By'. On Contact creation, I have to link an existing contact to it in the 'Referred By' Field.

Your trigger is running on both after insert and after update events, and you don't have any logic to distinguish between them. What's happening in your logs, I believe, is that you're looking at the after update log for the related Contact, not for the after insert event on the original Contact.

It looks like this trigger does not perform any Contact updates, but some other automation - perhaps another trigger, or a Process - is apparently updated the referring Contact and causing this trigger to execute again, in after update context.

I have two general recommendations for you:

  1. Use a trigger handler framework. It will dramatically increase your ability to understand and cleanly separate the flow of execution through your trigger.
  2. Make sure your triggers are idempotent - that is, if they're run multiple times on the same records, they have the same effect and don't duplicate effort. It looks like you are most of the way there, but you may have some work to do on your first if and on your task update operation to check first that you need to do work. Then, you'll be proof against recursion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.