You need to sign in to do that
Don't have an account?
Select statement for account is not returning any record in trigger on Task
Hi All,
Trigger On Task
trigger AddTaskActivityToAccountTLA on Task (after insert) {
for (Task task: Trigger.new){
if(task.WhatId!=null){
List<Account> accounts = [select id from Account];
system.debug('Size------------>'+accounts.size());
//List<Account> accounts = [select Name from Account where id =: task.WhatId];
//system.debug('Size------------>'+accounts.size());
if(accounts.size() > 0){
system.debug('accounts[0].id------->'+accounts[0].id);
AccountTLA__c[] accountTLA = [select id from AccountTLA__c where Account__c=: accounts[0].id];
system.debug('accountTLA.size()------->'+accountTLA.size());
if(accountTLA.size()>0){
system.debug('account[0].id------->'+accountTLA[0].id);
Task newtask= new Task(Type=task.Type,OwnerId=task.OwnerId,Subject=task.Subject,ActivityDate=task.ActivityDate,Status=task.Status,Priority=task.Priority,WhatId=accountTLA[0].id);
insert newtask;
}
}
}
}
}
I am not able to get account records in the trigger.where i am going wrong....
Best ,
Sree
Hi Sree
the way i see it, you have several issues with your code and logic.
regarding the code -
1.you are not ready for Bulk insert (what if two tasks are inserted at the same time - via data loader or something)
2.you are not checking if the task is even related to an account record - since you are creating tasks in your AFTER INSERT trigger - you potentially create an infinite loop.
regarding the logic -
1.what if two tasks are inserted for the same account, which one is "Copied" to the newly created task?
in any case, here is the code for your logic, ready for bulk and checking that the task is related to account (just complete the multiple tasks part)
Hope this helps.
if so, mark the issue as resolved.
Thanks
All Answers
Hi,
I have tried the following code and it is working fine. Please check when you are creating the a task in the relatedto Picklist the account object is selected.
trigger AddTaskActivityToAccountTLA on Task (after insert) {
for (Task task: Trigger.new){
if(task.WhatId!=null){
List<Account> accounts = [select id from Account];
system.debug('Size------------>'+accounts.size());
//List<Account> accounts = [select Name from Account where id =: task.WhatId];
//system.debug('Size------------>'+accounts.size());
if(accounts.size() > 0){
system.debug('accounts[0].id------->'+accounts[0].id);
//AccountTLA__c[] accountTLA = [select id from AccountTLA__c where Account__c=: accounts[0].id];
//system.debug('accountTLA.size()------->'+accountTLA.size());
//if(accountTLA.size()>0)
{
//system.debug('account[0].id------->'+accountTLA[0].id);
//Task newtask= new Task(Type=task.Type,OwnerId=task.OwnerId,Subject=task.Subject,ActivityDate=task.ActivityDate,Status=task.Status,Priority=task.Priority,WhatId=accountTLA[0].id);
//insert newtask;
}
}
}
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Hi Sree
the way i see it, you have several issues with your code and logic.
regarding the code -
1.you are not ready for Bulk insert (what if two tasks are inserted at the same time - via data loader or something)
2.you are not checking if the task is even related to an account record - since you are creating tasks in your AFTER INSERT trigger - you potentially create an infinite loop.
regarding the logic -
1.what if two tasks are inserted for the same account, which one is "Copied" to the newly created task?
in any case, here is the code for your logic, ready for bulk and checking that the task is related to account (just complete the multiple tasks part)
Hope this helps.
if so, mark the issue as resolved.
Thanks
Hi Noam,
I really thankfull to you. it's working fine for me.
Best
Sree