function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AlphaPAlphaP 

Trigger to Create Calendar Event (Loop troubles)

EDITIED:  Cleaned up code, added commnets and added some clarity

 

I'm working with a referal process where we work a lead one day and then get another list to call when documents are ready.   The first part of the trigger works - the part where i'm assigning the value of the External Unique ID to the lead company.  

 

For the rest - I want to find leads that have been converted - so that when I get the second lead for 'Docs Ready' - I can look at who converted the lead and create a calendar event for that person that contains some of the information on that customer.

 

So I pull a list of converted leads by matching the External Unique ID - they will already be in the system since this is a follow-up lead.  

 

I looked at the system.debug logs and the ConvertedLeadList never seesm to populate when I'm inseting test files.   Is it my blocking?  The query looks fine.  Any ideas?

 

Then what I would like to happen - is take the ConvertedLeadList and loop through that and create a calendar event based on data from that list.  One item I am unsure how to do is assign the calevent onwer based on the last modified id of the converted lead from the ConvertedLeadList.

 

How do I write the for loop for that?  I looked at the dev guide and i'm just not 'getting' it.

trigger Referral on Lead (before insert, before update) {

    //create a list of leads 'Converted:eadList' that looks for a mtach in the tmpEUIDset.  These converted leads have agent owners and will need to have a calendar event created.           
    List<Lead> ConvertedLeadList = new List<Lead>();
    Set<String> tmpEUIDset = new Set<String>();


//referals are external these - referals and do not have Party IDs - map EUID to party ID (company) on insert and exit loop        
        for(Lead l : trigger.new){         
        if((l.Lead_Type_ALL__c == 'LegalZoom Referral') || (l.Lead_Type_ALL__c == 'LegalZoom Docs Ready')) {
        l.company = l.External_Unique_ID__c;
                                
                           } //exit loop
                    
    
    //add EUIDS to string from insterted leads to match against when creating the ConvertedLeadList
    for(Lead lead : Trigger.new){
        tmpEUIDset.add(lead.External_Unique_ID__c);
            if(Lead.External_Unique_ID__c != null)
            system.debug('Here is the current count of items in the tmpEUIDset string:' + tmpEUIDset.size());
           
                          } // exit loop                  
    
// pull a list of converted leads from the first referral call by querying leads aginst the tmpEUID list
ConvertedLeadList = [select id, Lead_Type_ALL__c, External_Unique_ID__c, Lastmodifiedbyid, Name from Lead where SystemModstamp=LAST_N_DAYS:90 AND name in :tmpEUIDSet AND Lead.Status='Contacted' AND Isconverted=true];
system.debug('Here is the current ConvertedLeadList size' + Convertedleadlist.size());

    
//look for an existing opportunity when there is a 'Docs Ready' lead type

            if(ConvertedLeadList.size()>0){
                
            for(Lead mylead : trigger.new) {
                 if ((trigger.new[0].External_Unique_ID__c == ConvertedLeadList[0].External_Unique_ID__c) && (trigger.new[0].Lead_Type_ALL__c == 'LegalZoom Docs Ready')){
                     System.debug('Matching Converted lead record found - creating event for' + trigger.new[0]);
                    //This will generate an event for the current user if one does not exist currently
                    List<Event> checkEvent = [select id from event where whoid  =: Trigger.new[0].Id AND ActivityDateTime =: Trigger.new[0].Call_Back_Requested_All__c];
                        if(checkEvent == null || checkEvent.size() == 0){
                            //create new event
                            Event calEvent = new Event();
                            //base date vand time on Call_Back_Requested_ALL__c field on inserted lead
                            calEvent.ActivityDateTime = Trigger.new[0].Call_Back_Requested_All__c;
                            calEvent.Description = 'Documents are ready. Call back customer ' + Trigger.new[0].firstname + ' ' + Trigger.new[0].lastname +
                                        ' with Lead Party ID ' + Trigger.new[0].Company + '.';
                            calEvent.Subject = 'Call back customer ' + Trigger.new[0].firstname + ' ' + Trigger.new[0].lastname;
                            calEvent.DurationInMinutes = 15;
                            calEvent.ReminderDateTime = Trigger.new[0].Call_Back_Requested_All__c.addMinutes(-15);
                            // the cal event needs to go to agent that converted the lead
                            calEvent.Ownerid = ConvertedLeadList[0].lastmodifiedbyid;
                            calEvent.IsReminderSet = true;
                            calEvent.Whoid  = Trigger.new[0].Id;
                            insert calEvent;
                         
                            
                        }
                    }
                }
             }
          }
    }

 

AlphaPAlphaP

Edited to add some code comments, format the code better and provide a little clarity.