0

Can someone tell me how to tweak my code so the email alerts are sent out when my list of tasks is inserted?

global class  sched_TeamMemberExpiration implements Schedulable {

    //Make class schedulable    
    global void execute(SchedulableContext ctx) {        
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime
                          FROM CronTrigger WHERE Id = :ctx.getTriggerId()];

        //Get List of Team Members
        list<Account_Counselor__c> TeamMembers = new list<Account_Counselor__c>();
        date d                      = system.today().addDays(-60);
        date d2                     = system.today().addDays(-14);

        TeamMembers                 =[Select Fields and Criteria];

        List<Task> taskList = new List<Task>();
        RecordType TaskType = [Select Id from RecordType WHERE Name = 'Account Admin Task'];       

        //Loop through Team Members
        for(Account_Counselor__c TM:TeamMembers){

            string lastdate = null;

            //Fix Date
            if(tm.Last_Overall_Date__c != null){
                lastdate = tm.Last_Overall_Date__c.format();
            } else {
                lastdate = 'Never';
            }

              Task t            = new Task();
              t.Subject         = 'Inactive Team Member detected! - ' + TM.Hub_Name__r.Name;
              t.Type            = 'Account Administration';

              t.Description     = TM.Hub_Name__r.Name + 
                                    ' is no longer active on this account.  Their last activity on this account was on: ' + 
                                    lastdate + 
                                    '. \r\n' + 'Please remove this employee from the Team Members section of the Account page in CRM, as well as from any other applicable enrollment or client systems.';

              t.WhatId          = TM.Account__c;
              t.RecordTypeId    = TaskType.Id;
              t.OwnerId         = TM.Account__r.Backup_Owner__c;

              taskList.add(t);


        } //End for each Team Member loop

        insert taskList;  

    } //end schedudle class
} //end main class
0

I figured it out. Here is how you do it for other searchers.

for(Account_Counselor__c TM:TeamMembers){

            string lastdate = null;

            //Fix Date
            if(tm.Last_Overall_Date__c != null){
                lastdate = tm.Last_Overall_Date__c.format();
            } else {
                lastdate = 'Never';
            }

            Task t              = new Task();
            t.Subject           = 'Inactive Team Member detected! - ' + TM.Hub_Name__r.Name;
            t.Type          = 'Account Administration';

            t.Description       = TM.Hub_Name__r.Name + 
                ' is no longer active on this account.  Their last activity on this account was on: ' + 
                lastdate + 
                '. \r\n' + 'Please remove this employee from the Team Members section of the Account page in CRM, as well as from any other applicable enrollment or client systems.';

            t.WhatId            = TM.Account__c;
            t.RecordTypeId  = TaskType.Id;
            t.OwnerId           = TM.Account__r.Backup_Owner__c;

            Database.DMLOptions dlo = new Database.DMLOptions();
            dlo.EmailHeader.triggerUserEmail = TRUE;
            t.setOptions(dlo);

            taskList.add(t);


        } //End for each Team Member loop

        insert taskList;  

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.