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
Sree_salesforceSree_salesforce 

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

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Noam.dganiNoam.dgani

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)

trigger AddTaskActivityToAccountTLA on Task (after insert) 
{
	Map<String,Schema.SobjectType> globalDescribe = Schema.getGlobalDescribe();
	String accPref = globalDescribe.get('Account').getDescribe().getkeyprefix();
	map<Id,List<Task>> accIds = new map<Id,List<Task>>();
	List<Task> newTasks = new List<Task>();
	
	/*
		iterate over trigger records -
		if a task is related to an account, put its account Id in a set for later
	*/
	for(Task t : trigger.new)
		if(t.WhatId != null && String.valueOf(t.WhatId).subString(0,3).equalsIgnoreCase(accPref))
			if(!accIds.containsKey(t.WhatId))
			{
				accIds.put(t.WhatId,new List<Task>{t});
			}
				
	//if any of the tasks inserted are related to an account - perform your logic
	if(!accIds.isEmpty())
	{
		List<AccountTLA__c> lst_accTLAs;
		try{
			lst_accTLAs = [select Id,Account__c from AccountTLA__c where Account__c IN: accIds.keySet()];
		}
		catch(QueryException e)
		{
			//found no account TLAs for the relevant accounts
		}
		
		if(lst_accTLAs != null && !lst_accTLAs.isEmpty())
		{
			for(AccountTLA__c accTLA : lst_accTLAs)
			{
				List<Task> account_newTasks = accIds.get(accTLA.Account__c);
				if(account_newTasks != null && !account_newTasks.isEmpty())
				{
					if(account_newTasks.size() > 1)
					{
						//what values do you want to give the new task you are creating, 
						//if more than one task was inserted in the bulk for this account???
					}
					else
					{
						//I'm assigning the fields that you mentioned in your post, but if the business scenario allows it
						// cloning the original task and just changing the whatId before insert would be easier!!!
						Task t = new Task(
											Type = account_newTasks[0].Type,
											OwnerId = account_newTasks[0].OwnerId,
											Subject = account_newTasks[0].Subject,
											ActivityDate = account_newTasks[0].ActivityDate,
											Status = account_newTasks[0].Status,
											Priority = account_newTasks[0].Priority,
											WhatId = accTLA.Id;
										 );
						newTasks.add(t);
					}
				}
				
			}
		}
		if(!newTasks.isEmpty())
			insert newTasks;
	}
}

 

 

Hope this helps.

if so, mark the issue as resolved.

Thanks

All Answers

Navatar_DbSupNavatar_DbSup

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. 

 

Noam.dganiNoam.dgani

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)

trigger AddTaskActivityToAccountTLA on Task (after insert) 
{
	Map<String,Schema.SobjectType> globalDescribe = Schema.getGlobalDescribe();
	String accPref = globalDescribe.get('Account').getDescribe().getkeyprefix();
	map<Id,List<Task>> accIds = new map<Id,List<Task>>();
	List<Task> newTasks = new List<Task>();
	
	/*
		iterate over trigger records -
		if a task is related to an account, put its account Id in a set for later
	*/
	for(Task t : trigger.new)
		if(t.WhatId != null && String.valueOf(t.WhatId).subString(0,3).equalsIgnoreCase(accPref))
			if(!accIds.containsKey(t.WhatId))
			{
				accIds.put(t.WhatId,new List<Task>{t});
			}
				
	//if any of the tasks inserted are related to an account - perform your logic
	if(!accIds.isEmpty())
	{
		List<AccountTLA__c> lst_accTLAs;
		try{
			lst_accTLAs = [select Id,Account__c from AccountTLA__c where Account__c IN: accIds.keySet()];
		}
		catch(QueryException e)
		{
			//found no account TLAs for the relevant accounts
		}
		
		if(lst_accTLAs != null && !lst_accTLAs.isEmpty())
		{
			for(AccountTLA__c accTLA : lst_accTLAs)
			{
				List<Task> account_newTasks = accIds.get(accTLA.Account__c);
				if(account_newTasks != null && !account_newTasks.isEmpty())
				{
					if(account_newTasks.size() > 1)
					{
						//what values do you want to give the new task you are creating, 
						//if more than one task was inserted in the bulk for this account???
					}
					else
					{
						//I'm assigning the fields that you mentioned in your post, but if the business scenario allows it
						// cloning the original task and just changing the whatId before insert would be easier!!!
						Task t = new Task(
											Type = account_newTasks[0].Type,
											OwnerId = account_newTasks[0].OwnerId,
											Subject = account_newTasks[0].Subject,
											ActivityDate = account_newTasks[0].ActivityDate,
											Status = account_newTasks[0].Status,
											Priority = account_newTasks[0].Priority,
											WhatId = accTLA.Id;
										 );
						newTasks.add(t);
					}
				}
				
			}
		}
		if(!newTasks.isEmpty())
			insert newTasks;
	}
}

 

 

Hope this helps.

if so, mark the issue as resolved.

Thanks

This was selected as the best answer
Sree_salesforceSree_salesforce

Hi Noam,

 I really thankfull to you. it's working fine for me.

 

 

Best

Sree