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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Salesforce Support Couldnt help - Mulltiple Task creation and Time based workflows

Hi There, 

I have an issue, I have a timebased workflow which is designed to create tasks at certain periods of time within a 12 month period for the account owner, this is based on a clients support expiration. I came into an issue when it started creating multiple versions of the same task, not just for the account owner but for other users(users that do not even have access to the accounts that they have had tasks created for). 

 

I submitted a case and was told that the issue is more than likely within my task trigger, obviously this wasnt very helpful. This is an annoying bug as my users are constantly getting notifications for accounts that they cannot even access, this looks bad on me. I do not see how my code could be creating multiple tasks, I feel like it is a brush off.

This is my task trigger, the only bit that pertains to the created tasks records appears in the before insert trigger, it records the task.whatid as it is originally created from the service object, it reassigns it to account.

Please help, I am so lost. Any help would be much appreciated.


 

trigger TaskTrigger on Task(before update,before insert, before delete, after update, after insert, after delete) {



//Create Lists
List<Account_Status__c> AccSt = new List<Account_Status__c>();
List<Account_Status__c> updatedAccSt = new List<Account_Status__c>();

List<Service__c> SerList = new List<Service__c>();
List<Task> TaskUpdate = new List<Task>();
List<Id> Serids = new List<Id>();

List<Id> accIds = new List<Id>();
//Account list for adding associated contacts
List<Id> AccountIds = new List<Id>();
List<Task> TaskToInsert = new List<Task>();
//Map Record Types
Schema.DescribeSObjectResult d = Task.sObjectType.getDescribe();
Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();

//Upload Authority Form Map
Schema.RecordTypeInfo UploadRecType =  rtMapByName.get('Upload Authority Form');
Id UploadRecId = UploadRecType.getRecordTypeId();

//Initial Contact Prospective Client Map
Schema.RecordTypeInfo ContactRecType =  rtMapByName.get('Initial Contact Prospective Client');
Id InitialRecId = ContactRecType.getRecordTypeId();

//Initial Contact Prospective Client - Follow Up Map
Schema.RecordTypeInfo ContactFollowRecType =  rtMapByName.get('Initial Contact Prospective Client - Follow Up');
Id InitialFollowRecId = ContactRecType.getRecordTypeId();

//Arrange Support Review Map
Schema.RecordTypeInfo ReviewRecType =  rtMapByName.get('Arrange Support Review');
Id ReviewRecId = ReviewRecType.getRecordTypeId();

if(trigger.isbefore){
if(trigger.isdelete){

//Delete prevention
 String ProfileId = UserInfo.getProfileId();  
   List<Profile> profiles=[select id from Profile where name='System Administrator'];
   

   if (1!=profiles.size())
   {
      // unable to get the profiles - handle error
   }
   else
   {
       for (Task a : Trigger.old)      
       {            
          if ( profileId!=profiles[0].id )
          {
          
          
           a.addError('You do not have the level of access to delete this Activity Record. Please contact your system Administrator or set the Task to "Completed"');
          }
       }            
   }



}
if(trigger.isinsert){
//before insert logic




//Upload Authority Form Record RecordType field set
for (Task Tas: trigger.new){


//Call Prospective Client RecordSet
if (Tas.Subject == 'Call Prospective Client') {
Tas.RecordTypeID = InitialRecId;
Tas.Type = 'Call';
Tas.Status = 'Not Started';
if (Tas.Priority == 'High') {
Tas.Assigned_to__c = True;
}
tas.IsReminderSet = True;
tas.ReminderDateTime = system.now();


}

//Arrange Support Review RecordSet
if((Tas.Subject == 'Arrange Support Review'||Tas.Subject == 'Arrange Support Review90Days'||Tas.Subject == 'Arrange Support Review180Days'||Tas.Subject == 'Arrange Support Review270Days'||Tas.Subject == 'Arrange Support Review30Days'||Tas.Subject == 'Arrange Support Review60Days')&&Tas.Priority == 'Normal') {
SerIds.add(tas.whatId);

}
//Upload Authoirty Form RecordSet Support Continuation
if(tas.WhatId != null && Tas.Subject == 'Upload Authority Form_Continuation') {
Tas.RecordTypeID = UploadRecId;
Tas.Type = 'Document Upload';
Tas.Status = 'Awaiting Form';
tas.IsReminderSet = True;
tas.ReminderDateTime = system.now();

}
//Upload Authority Form RecordSet
if(tas.WhatId != null && Tas.Subject == 'Upload Authority Form') {
Tas.RecordTypeID = UploadRecId;
Tas.Type = 'Document Upload';
Tas.Status = 'Awaiting Form';
tas.IsReminderSet = True;
tas.ReminderDateTime = system.now();
accIds.add(tas.whatId);
}


}
//Find related Account Status record
AccSt = [Select Id,Account_Status__c from Account_Status__c WHERE Account__c in :accIds];
SerList = [select id, Account__c from Service__c where id in :SerIds];

for (Task Tas: trigger.new){
for(Service__c Ser : SerList ){
if ((Tas.Subject == 'Arrange Support Review'||Tas.Subject == 'Arrange Support Review90Days'||Tas.Subject == 'Arrange Support Review180Days'||Tas.Subject == 'Arrange Support Review270Days'||Tas.Subject == 'Arrange Support Review30Days'||Tas.Subject == 'Arrange Support Review60Days')&&Tas.Priority == 'Normal') {
Tas.RecordTypeID = ReviewRecId;
Tas.Type = 'Call';
Tas.Status = 'Not Started';
tas.IsReminderSet = True;
tas.ReminderDateTime = system.now();
Tas.Whatid = Ser.Account__c;
}
}
}

for(Account_Status__c a : AccSt)
{
//Set Account Status and input Date
a.Account_Status__c = 'Service Decision Yes: Awaiting Authority Form';
a.Decision_Yes_Awaiting_Authority_Form__c =  date.today();
updatedAccSt.add(a);
}




//Check that Account Status List has been altered and update
if(updatedAccSt.size()>0)
{
update updatedAccSt;
}
}



else if(trigger.isupdate){

//before update logic
for (Task  tas: trigger.new) {
if(tas.WhatId != null && (Tas.Subject == 'Call Prospective Client'||Tas.subject == 'Call Prospective Client - Follow Up')) {
accIds.add(tas.whatId);
}
if(tas.WhatId != null && Tas.Subject == 'Upload Authority Form') {
accIds.add(tas.whatId);
}
if(tas.WhatId != null && Tas.Subject == 'Destiny Presentation Follow Up') {
accIds.add(tas.whatId);
}
}
//Find related Account Status record
AccSt = [Select Id,Account_Status__c from Account_Status__c WHERE Account__c in :accIds];
for(Account_Status__c a : AccSt)
{
for (Task  tas: trigger.new) {
if(Tas.Appointment_Decision__c == 'No' && Tas.Status == 'Completed') {
a.Account_Status__c = 'Enquiry Contacted by Office: No Presentation';
a.Enquiry_Contacted_No_Presentation__c =  date.today();
}
if(Tas.Appointment_Decision__c == 'Maybe'  && Tas.Status == 'Completed') {
Datetime dt = Tas.Follow_Up_Datetime__c;

a.Account_Status__c = 'Enquiry Contacted by Office: Awaiting Follow up';
a.Enquiry_Contacted_by_Office_Maybe__c =  date.today();

 //Create Follow Up Task
       Task Task = new Task();
       Task.RecordTypeID = InitialFollowRecId;
       Task.subject = 'Call Prospective Client - Follow Up';
       Task.Type = 'Call';
       Task.Status = 'Not Started';
       Task.ActivityDate = Tas.Follow_Up_Datetime__c.date();
       Task.Follow_Up_Datetime__c = Tas.Follow_Up_Datetime__c;
       Task.WhatId = Tas.WhatId;
       Task.OwnerId = Tas.OwnerId;
       If(Tas.WhoID != null){
       Task.Whoid = Tas.whoid;
       task.IsReminderSet = True;
       task.ReminderDateTime = system.now();
       }
       taskToInsert.add(Task);

}
if(Tas.Appointment_Decision__c == 'Yes'  && Tas.Status == 'Completed') {
a.Account_Status__c = 'Presentation Decision - Yes: Awaiting Booking';
a.Presentation_Decision_Yes_Awaiting_Book__c =  date.today();
}
if(Tas.Status == 'Document Uploaded') {
a.Account_Status__c = 'Authority Form received Office: Uploaded to Account';
a.Authority_Form_received_Office_Uploaded__c =  date.today();
}
If(Tas.Status == 'Not Proceeding'){
a.Account_Status__c = 'Destiny Presentation: Service Decision - No';
a.Destiny_Presentation_Service_Decision_N0__c = date.today();

}
if(Tas.Destiny_Service_Decision__c == 'No' && Tas.Subject == 'Destiny Presentation Follow Up'  && Tas.Status == 'Completed') {
       a.Account_Status__c = 'Destiny Presentation Follow-up: Service Decision - No';
       a.Destiny_Presentation_Service_Decision_N0__c = date.today();
       } 
}
updatedAccSt.add(a);

}
if(updatedAccSt.size()>0){

update updatedAccSt;
}
insert taskToInsert;
}
}

else if(trigger.isafter){
if(trigger.isinsert){
//after Insert logic


    }

}

}