You need to sign in to do that
Don't have an account?

Task Not Creating
This task isn't creating and I'm not sure why. I know it isn't bulkified. Any help is appreciated.
trigger QBRTasks on Task (after update) { // Get the record type Id for QBR tasks Task qbrTaskRecordTypeId = [SELECT Id FROM Task WHERE RecordType.DeveloperName = 'Executive_Business_Review' LIMIT 1]; for (Task myTask : Trigger.new) { Account acc = [SELECT Id, Customer_Segment__c, Task_Counter__c, Account_Manager__c, Name, Type FROM Account WHERE Id = :myTask.AccountId LIMIT 1]; // Get trigger.old and trigger.new versions of the record Task oldTask = Trigger.oldMap.get(myTask.Id); Task newTask = Trigger.newMap.get(myTask.Id); // Compare statuses on old and new triggers to see if it changed to Completed Boolean taskCompleted = (oldTask.Status != 'Completed' && newTask.Status == 'Completed'); // See if task is completed and a QBR/check-in task if (myTask.RecordTypeId == qbrTaskRecordTypeId.Id && taskCompleted && acc.Type == 'Customer') { // High Touch - 2nd Check-in task if (acc.Customer_Segment__c == 'High Touch' && acc.Task_Counter__c == 1) { Task ht2 = new Task(); ht2.ActivityDate = date.today() + 10; ht2.OwnerId = acc.Account_Manager__c; ht2.Priority = 'Normal'; ht2.RecordTypeId = qbrTaskRecordTypeId.Id; ht2.Status = 'Not Started'; ht2.Subject = '2nd Check-in Call + ' + acc.Name; ht2.WhatId = acc.Id; insert ht2; acc.Task_Counter__c = 2; update acc; } // Mid Touch - 2nd Check-in task if (acc.Customer_Segment__c == 'Mid Touch' && acc.Task_Counter__c == 1) { Task mt2 = new Task(); mt2.ActivityDate = date.today() + 14; mt2.OwnerId = acc.Account_Manager__c; mt2.Priority = 'Normal'; mt2.RecordTypeId = qbrTaskRecordTypeId.Id; mt2.Status = 'Not Started'; mt2.Subject = '2nd Check-in Call + ' + acc.Name; mt2.WhatId = acc.Id; insert mt2; acc.Task_Counter__c = 2; update acc; } // High Touch - 3rd Check-in task if (acc.Customer_Segment__c == 'High Touch' && acc.Task_Counter__c == 2) { Task ht3 = new Task(); ht3.ActivityDate = date.today() + 10; ht3.OwnerId = acc.Account_Manager__c; ht3.Priority = 'Normal'; ht3.RecordTypeId = qbrTaskRecordTypeId.Id; ht3.Status = 'Not Started'; ht3.Subject = '3rd Check-in Call + ' + acc.Name; ht3.WhatId = acc.Id; insert ht3; acc.Task_Counter__c = 3; update acc; } // Mid Touch = 3+ QBRs if (acc.Customer_Segment__c == 'Mid Touch' && acc.Task_Counter__c >= 2) { Task mt3 = new Task(); mt3.ActivityDate = date.today() + 180; mt3.OwnerId = acc.Account_Manager__c; mt3.Priority = 'Normal'; mt3.RecordTypeId = qbrTaskRecordTypeId.Id; mt3.Status = 'Not Started'; mt3.Subject = 'Semi-annual QBR + ' + acc.Name; mt3.WhatId = acc.Id; insert mt3; acc.Task_Counter__c = acc.Task_Counter__c; update acc; } // High Touch - 4th QBR task if (acc.Customer_Segment__c == 'High Touch' && acc.Task_Counter__c == 3) { Task ht4 = new Task(); ht4.ActivityDate = date.today() + 60; ht4.OwnerId = acc.Account_Manager__c; ht4.Priority = 'Normal'; ht4.RecordTypeId = qbrTaskRecordTypeId.Id; ht4.Status = 'Not Started'; ht4.Subject = 'QBR + ' + acc.Name; ht4.WhatId = acc.Id; insert ht4; acc.Task_Counter__c = 4; update acc; } // High Touch - 5+ QBR Tasks if (acc.Customer_Segment__c == 'High Touch' && acc.Task_Counter__c >= 4) { Task ht5 = new Task(); ht5.ActivityDate = date.today() + 90; ht5.OwnerId = acc.Account_Manager__c; ht5.Priority = 'Normal'; ht5.RecordTypeId = qbrTaskRecordTypeId.Id; ht5.Status = 'Not Started'; ht5.Subject = 'QBR + ' + acc.Name; ht5.WhatId = acc.Id; insert ht5; acc.Task_Counter__c = acc.Task_Counter__c + 1; update acc; } } } }
Try this code and this code is also bulkify.
trigger QBRTasks on Task (after update) {
Task qbrTaskRecordTypeId = [SELECT Id
FROM Task
WHERE RecordType.DeveloperName = 'Executive_Business_Review'
LIMIT 1];
List<Task> finalIns = new List<Task>() ;
List<Account> finalAccUp = new List<Account>();
set<id> AccId=new set<id>();
for(Task tsk:trigger.new){
if(tsk.whatid!=null && what.type = 'Account')
AccId.add(tsk.Accountid);
}
for (Task myTask : Trigger.new) {
for(Account acc:[SELECT Id,
Customer_Segment__c,
Task_Counter__c,
Account_Manager__c,
Name,
Type
FROM Account
WHERE Id in:AccId ]{
// Get trigger.old and trigger.new versions of the record
Task oldTask = Trigger.oldMap.get(myTask.Id);
Task newTask = Trigger.newMap.get(myTask.Id);
// Compare statuses on old and new triggers to see if it changed to Completed
Boolean taskCompleted = (oldTask.Status != 'Completed' && newTask.Status == 'Completed');
// See if task is completed and a QBR/check-in task
if (myTask.RecordTypeId == qbrTaskRecordTypeId.Id && taskCompleted && acc.Type == 'Customer') {
// High Touch - 2nd Check-in task
if (acc.Customer_Segment__c == 'High Touch' && acc.Task_Counter__c == 1) {
Task ht2 = new Task();
ht2.ActivityDate = date.today() + 10;
ht2.OwnerId = acc.Account_Manager__c;
ht2.Priority = 'Normal';
ht2.RecordTypeId = qbrTaskRecordTypeId.Id;
ht2.Status = 'Not Started';
ht2.Subject = '2nd Check-in Call + ' + acc.Name;
ht2.WhatId = acc.Id;
finalIns.add(ht2) ;
acc.Task_Counter__c = 2;
finalAccUp.add(acc);
}
// Mid Touch - 2nd Check-in task
if (acc.Customer_Segment__c == 'Mid Touch' && acc.Task_Counter__c == 1) {
Task mt2 = new Task();
mt2.ActivityDate = date.today() + 14;
mt2.OwnerId = acc.Account_Manager__c;
mt2.Priority = 'Normal';
mt2.RecordTypeId = qbrTaskRecordTypeId.Id;
mt2.Status = 'Not Started';
mt2.Subject = '2nd Check-in Call + ' + acc.Name;
mt2.WhatId = acc.Id;
finalIns.add(mt2) ;
acc.Task_Counter__c = 2;
finalAccUp.add(acc);
}
// High Touch - 3rd Check-in task
if (acc.Customer_Segment__c == 'High Touch' && acc.Task_Counter__c == 2) {
Task ht3 = new Task();
ht3.ActivityDate = date.today() + 10;
ht3.OwnerId = acc.Account_Manager__c;
ht3.Priority = 'Normal';
ht3.RecordTypeId = qbrTaskRecordTypeId.Id;
ht3.Status = 'Not Started';
ht3.Subject = '3rd Check-in Call + ' + acc.Name;
ht3.WhatId = acc.Id;
finalIns.add(ht3);
acc.Task_Counter__c = 3;
finalAccUp.add(acc);
}
// Mid Touch = 3+ QBRs
if (acc.Customer_Segment__c == 'Mid Touch' && acc.Task_Counter__c >= 2) {
Task mt3 = new Task();
mt3.ActivityDate = date.today() + 180;
mt3.OwnerId = acc.Account_Manager__c;
mt3.Priority = 'Normal';
mt3.RecordTypeId = qbrTaskRecordTypeId.Id;
mt3.Status = 'Not Started';
mt3.Subject = 'Semi-annual QBR + ' + acc.Name;
mt3.WhatId = acc.Id;
finalIns.add(mt3) ;
acc.Task_Counter__c = acc.Task_Counter__c;
finalAccUp.add(acc);
}
// High Touch - 4th QBR task
if (acc.Customer_Segment__c == 'High Touch' && acc.Task_Counter__c == 3) {
Task ht4 = new Task();
ht4.ActivityDate = date.today() + 60;
ht4.OwnerId = acc.Account_Manager__c;
ht4.Priority = 'Normal';
ht4.RecordTypeId = qbrTaskRecordTypeId.Id;
ht4.Status = 'Not Started';
ht4.Subject = 'QBR + ' + acc.Name;
ht4.WhatId = acc.Id;
finalIns.add(ht4) ;
acc.Task_Counter__c = 4;
finalAccUp.add(acc);
}
// High Touch - 5+ QBR Tasks
if (acc.Customer_Segment__c == 'High Touch' && acc.Task_Counter__c >= 4) {
Task ht5 = new Task();
ht5.ActivityDate = date.today() + 90;
ht5.OwnerId = acc.Account_Manager__c;
ht5.Priority = 'Normal';
ht5.RecordTypeId = qbrTaskRecordTypeId.Id;
ht5.Status = 'Not Started';
ht5.Subject = 'QBR + ' + acc.Name;
ht5.WhatId = acc.Id;
finalIns.add(ht5) ;
acc.Task_Counter__c = acc.Task_Counter__c + 1;
finalAccUp.add(acc);
}
}
}
}
if(finalIns.size()>0){
insert finalIns;
}
if(finalAccUp.size()>0){
update finalAccUp;
}
}
Please mark this as Best Answer if you find this solution helpful.
Thank You
Ajay Dubedi
All Answers
Try this code and this code is also bulkify.
trigger QBRTasks on Task (after update) {
Task qbrTaskRecordTypeId = [SELECT Id
FROM Task
WHERE RecordType.DeveloperName = 'Executive_Business_Review'
LIMIT 1];
List<Task> finalIns = new List<Task>() ;
List<Account> finalAccUp = new List<Account>();
set<id> AccId=new set<id>();
for(Task tsk:trigger.new){
if(tsk.whatid!=null && what.type = 'Account')
AccId.add(tsk.Accountid);
}
for (Task myTask : Trigger.new) {
for(Account acc:[SELECT Id,
Customer_Segment__c,
Task_Counter__c,
Account_Manager__c,
Name,
Type
FROM Account
WHERE Id in:AccId ]{
// Get trigger.old and trigger.new versions of the record
Task oldTask = Trigger.oldMap.get(myTask.Id);
Task newTask = Trigger.newMap.get(myTask.Id);
// Compare statuses on old and new triggers to see if it changed to Completed
Boolean taskCompleted = (oldTask.Status != 'Completed' && newTask.Status == 'Completed');
// See if task is completed and a QBR/check-in task
if (myTask.RecordTypeId == qbrTaskRecordTypeId.Id && taskCompleted && acc.Type == 'Customer') {
// High Touch - 2nd Check-in task
if (acc.Customer_Segment__c == 'High Touch' && acc.Task_Counter__c == 1) {
Task ht2 = new Task();
ht2.ActivityDate = date.today() + 10;
ht2.OwnerId = acc.Account_Manager__c;
ht2.Priority = 'Normal';
ht2.RecordTypeId = qbrTaskRecordTypeId.Id;
ht2.Status = 'Not Started';
ht2.Subject = '2nd Check-in Call + ' + acc.Name;
ht2.WhatId = acc.Id;
finalIns.add(ht2) ;
acc.Task_Counter__c = 2;
finalAccUp.add(acc);
}
// Mid Touch - 2nd Check-in task
if (acc.Customer_Segment__c == 'Mid Touch' && acc.Task_Counter__c == 1) {
Task mt2 = new Task();
mt2.ActivityDate = date.today() + 14;
mt2.OwnerId = acc.Account_Manager__c;
mt2.Priority = 'Normal';
mt2.RecordTypeId = qbrTaskRecordTypeId.Id;
mt2.Status = 'Not Started';
mt2.Subject = '2nd Check-in Call + ' + acc.Name;
mt2.WhatId = acc.Id;
finalIns.add(mt2) ;
acc.Task_Counter__c = 2;
finalAccUp.add(acc);
}
// High Touch - 3rd Check-in task
if (acc.Customer_Segment__c == 'High Touch' && acc.Task_Counter__c == 2) {
Task ht3 = new Task();
ht3.ActivityDate = date.today() + 10;
ht3.OwnerId = acc.Account_Manager__c;
ht3.Priority = 'Normal';
ht3.RecordTypeId = qbrTaskRecordTypeId.Id;
ht3.Status = 'Not Started';
ht3.Subject = '3rd Check-in Call + ' + acc.Name;
ht3.WhatId = acc.Id;
finalIns.add(ht3);
acc.Task_Counter__c = 3;
finalAccUp.add(acc);
}
// Mid Touch = 3+ QBRs
if (acc.Customer_Segment__c == 'Mid Touch' && acc.Task_Counter__c >= 2) {
Task mt3 = new Task();
mt3.ActivityDate = date.today() + 180;
mt3.OwnerId = acc.Account_Manager__c;
mt3.Priority = 'Normal';
mt3.RecordTypeId = qbrTaskRecordTypeId.Id;
mt3.Status = 'Not Started';
mt3.Subject = 'Semi-annual QBR + ' + acc.Name;
mt3.WhatId = acc.Id;
finalIns.add(mt3) ;
acc.Task_Counter__c = acc.Task_Counter__c;
finalAccUp.add(acc);
}
// High Touch - 4th QBR task
if (acc.Customer_Segment__c == 'High Touch' && acc.Task_Counter__c == 3) {
Task ht4 = new Task();
ht4.ActivityDate = date.today() + 60;
ht4.OwnerId = acc.Account_Manager__c;
ht4.Priority = 'Normal';
ht4.RecordTypeId = qbrTaskRecordTypeId.Id;
ht4.Status = 'Not Started';
ht4.Subject = 'QBR + ' + acc.Name;
ht4.WhatId = acc.Id;
finalIns.add(ht4) ;
acc.Task_Counter__c = 4;
finalAccUp.add(acc);
}
// High Touch - 5+ QBR Tasks
if (acc.Customer_Segment__c == 'High Touch' && acc.Task_Counter__c >= 4) {
Task ht5 = new Task();
ht5.ActivityDate = date.today() + 90;
ht5.OwnerId = acc.Account_Manager__c;
ht5.Priority = 'Normal';
ht5.RecordTypeId = qbrTaskRecordTypeId.Id;
ht5.Status = 'Not Started';
ht5.Subject = 'QBR + ' + acc.Name;
ht5.WhatId = acc.Id;
finalIns.add(ht5) ;
acc.Task_Counter__c = acc.Task_Counter__c + 1;
finalAccUp.add(acc);
}
}
}
}
if(finalIns.size()>0){
insert finalIns;
}
if(finalAccUp.size()>0){
update finalAccUp;
}
}
Please mark this as Best Answer if you find this solution helpful.
Thank You
Ajay Dubedi