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
David Wright 97David Wright 97 

Trigger language update

Hi everyone,

I currently have no coding knowledge so hoping somebidy is able to provide some assistance.  

I have the below trigger which populates a custom actvity field, 'Task Created and Completed By Current AE', when a completed task was created by the current Account Owner.

It is currently firing on the 'WhatId' field being related to an Account.

Is somebody able to update the language to also include Contacts? So if a completed task is created against a contact, and the Account Owner of the related contact matches the task creator, then the 'Task Created and Completed By Current AE' is also populated?

Greatly appreciate any help that can be provided. 

Best regards
David      

/**This trigger updates the "Task Created and Completed by Current AE" checkbox when the Task Created by the Account Owner moves to "Completed" status**/

trigger updateTaskCompletedByAECheckBox on Task(before update,before insert)
{
  List<Id> acclist = new List<Id>();
  Set<Id> taskSet = new Set<Id>();
  for (Task tsk: Trigger.new)
  {
    //Check if the Task Status is Completed
    if((tsk.Status == 'Completed') && tsk.WhatId != null)
    {
      String s = (String) tsk.WhatId;
      if(s.substring(0,3) == '001')
      {
        accList.add(tsk.whatId);
        taskSet.add(tsk.Id); //Set that contains the Completed Tasks linked to accounts
      }  
    }
  
  }
  
  Map<Id,Account> accMap = new Map<Id,Account>([Select id,Name,OwnerId from Account where Id in:accList]);
  
  for (Task tsk: Trigger.new)
  {
    Account acc= accmap.get(tsk.WhatId);
    if(acc!=null)
    {
    Id AcctOwnerId = accmap.get(tsk.WhatId).ownerid;
    
      if(Trigger.isInsert)
      {
        if((taskSet.contains(tsk.Id)) && UserInfo.getUserId() == AcctOwnerId)  //If the Task was Created by the Account Owner and its is Completed, then check the checkbox
          tsk.Task_Created_and_Completed_By_Curent_AE__c = true;
      } 
      
      else if(Trigger.isUpdate)
      {
        if((taskSet.contains(tsk.Id)) && tsk.CreatedById == AcctOwnerId)  //If the Task was Created by the Account Owner and its is Completed, then check the checkbox
          tsk.Task_Created_and_Completed_By_Curent_AE__c = true;
      } 
         
    } 
 
  }

}
Best Answer chosen by David Wright 97
Abdul KhatriAbdul Khatri
Please try the below code
 
trigger updateTaskCompletedByAECheckBox on Task(before update,before insert)
{
	List<Id> acclist = new List<Id>();
	List<Id> conlist = new List<Id>();    
  	Set<Id> taskSet = new Set<Id>();
    
  	for (Task tsk: Trigger.new)
  	{
    	//Check if the Task Status is Completed
    	if(tsk.Status == 'Completed' && (tsk.WhatId != null || (tsk.WhatId == null && tsk.WhoId != null)))
    	{
            
            if(tsk.WhatId != null) {
      			String s = (String) tsk.WhatId;
                if(s.substring(0,3) == '001')
                {
                    accList.add(tsk.whatId);
                    taskSet.add(tsk.Id); //Set that contains the Completed Tasks linked to accounts
                }  
            } else if (tsk.WhoId != null) {
                String s = (String) (tsk.WhoId);
                if(s.substring(0,3) == '003') {
                    conList.add(tsk.WhoId);
                    taskSet.add(tsk.Id);
                }
            }
            
    	}
  
  	}

  	Map<Id,Account> accMap = new Map<Id,Account>([Select id,Name,OwnerId from Account where Id in:accList]);
    Map<Id,Contact> contMap = new Map<Id,Contact>([SELECT Account.OwnerId FROM Contact WHERE Id in:conList]);
    
    for (Task tsk: Trigger.new)
  	{
    	//Account acc= accmap.get(tsk.WhatId);
        Id AcctOwnerId;
        
        if(accMap.get(tsk.WhatId) != null) 
            AcctOwnerId = accMap.get(tsk.WhatId).ownerid;
        else if (contmap.get(tsk.WhoId) != null)
            AcctOwnerId = contmap.get(tsk.WhoId).Account.OwnerId;
        
    	if(AcctOwnerId!=null)
    	{
    		//Id AcctOwnerId = accmap.get(tsk.WhatId).ownerid;
    
            if(Trigger.isInsert)
            {
                if((taskSet.contains(tsk.Id)) && UserInfo.getUserId() == AcctOwnerId)  //If the Task was Created by the Account Owner and its is Completed, then check the checkbox
                    tsk.Task_Created_and_Completed_By_Curent_AE__c = true;
            } 
            else if(Trigger.isUpdate)
            {
                if((taskSet.contains(tsk.Id)) && tsk.CreatedById == AcctOwnerId)  //If the Task was Created by the Account Owner and its is Completed, then check the checkbox
                tsk.Task_Created_and_Completed_By_Curent_AE__c = true;
            } 
         
    	} 
 
  	}

}

 

All Answers

Abdul KhatriAbdul Khatri
Please try the below code
 
trigger updateTaskCompletedByAECheckBox on Task(before update,before insert)
{
	List<Id> acclist = new List<Id>();
	List<Id> conlist = new List<Id>();    
  	Set<Id> taskSet = new Set<Id>();
    
  	for (Task tsk: Trigger.new)
  	{
    	//Check if the Task Status is Completed
    	if(tsk.Status == 'Completed' && (tsk.WhatId != null || (tsk.WhatId == null && tsk.WhoId != null)))
    	{
            
            if(tsk.WhatId != null) {
      			String s = (String) tsk.WhatId;
                if(s.substring(0,3) == '001')
                {
                    accList.add(tsk.whatId);
                    taskSet.add(tsk.Id); //Set that contains the Completed Tasks linked to accounts
                }  
            } else if (tsk.WhoId != null) {
                String s = (String) (tsk.WhoId);
                if(s.substring(0,3) == '003') {
                    conList.add(tsk.WhoId);
                    taskSet.add(tsk.Id);
                }
            }
            
    	}
  
  	}

  	Map<Id,Account> accMap = new Map<Id,Account>([Select id,Name,OwnerId from Account where Id in:accList]);
    Map<Id,Contact> contMap = new Map<Id,Contact>([SELECT Account.OwnerId FROM Contact WHERE Id in:conList]);
    
    for (Task tsk: Trigger.new)
  	{
    	//Account acc= accmap.get(tsk.WhatId);
        Id AcctOwnerId;
        
        if(accMap.get(tsk.WhatId) != null) 
            AcctOwnerId = accMap.get(tsk.WhatId).ownerid;
        else if (contmap.get(tsk.WhoId) != null)
            AcctOwnerId = contmap.get(tsk.WhoId).Account.OwnerId;
        
    	if(AcctOwnerId!=null)
    	{
    		//Id AcctOwnerId = accmap.get(tsk.WhatId).ownerid;
    
            if(Trigger.isInsert)
            {
                if((taskSet.contains(tsk.Id)) && UserInfo.getUserId() == AcctOwnerId)  //If the Task was Created by the Account Owner and its is Completed, then check the checkbox
                    tsk.Task_Created_and_Completed_By_Curent_AE__c = true;
            } 
            else if(Trigger.isUpdate)
            {
                if((taskSet.contains(tsk.Id)) && tsk.CreatedById == AcctOwnerId)  //If the Task was Created by the Account Owner and its is Completed, then check the checkbox
                tsk.Task_Created_and_Completed_By_Curent_AE__c = true;
            } 
         
    	} 
 
  	}

}

 
This was selected as the best answer
David Wright 97David Wright 97
Thank you, will try and confirm if successful.

Best regards
David
David Wright 97David Wright 97
Abdul, thank you your update works as requested.

I had forgot to also include in my request that when an Account Owner is changed any related tasks with the field, 'Task Created and Completed By Current AE' checked be removed.

Are you able to include the text for this?

Again greatly appreciate your help and expertise.

Regards
David  
Abdul KhatriAbdul Khatri
Sure will provide you the solution. How about the one already provided. Is that worked as expected? Did you try?
David Wright 97David Wright 97
Yes see my first line above. Fantastic support here!

Appreciate your help.
Abdul KhatriAbdul Khatri
Oops overlooked. Thanks
Abdul KhatriAbdul Khatri
Please try out this
 
trigger accountOwnerChangeTaskUpdateTrigger on Account (after update) {
    
    Set<Id> idAcctOwnerSet = new Set<Id>();
    
    for(Account acct : Trigger.new) {
        
        if(acct.OwnerId == Trigger.oldMap.get(acct.Id).OwnerId) continue;
        
        idAcctOwnerSet.add(Trigger.oldMap.get(acct.Id).OwnerId);
    }

    if(idAcctOwnerSet.isEmpty()) return;
    
    List<Task> taskList = [SELECT Task_Created_and_Completed_By_Curent_AE__c FROM Task WHERE CreatedById IN :idAcctOwnerSet AND Task_Created_and_Completed_By_Curent_AE__c = true];
    
    if(taskList == null) return;
    
    for(Task task : taskList) {
        
        task.Task_Created_and_Completed_By_Curent_AE__c = false;
    }
    
    update taskList;
    
}

 
David Wright 97David Wright 97
Thank you Abdul.

Is it possible to embed this additional text into your original script?

Best regards
David
Abdul KhatriAbdul Khatri
Not sure what do you mean by that but both trigger on a different objects based on what user does.
  • The first (top) one works when there is any action happened on the Task Object.
  • The last happend when action happened on the Account Object.
Therefore we cannot merge them.
David Wright 97David Wright 97
Sorry Abdul, I can see that my initial message did not post.

I am receiving this error message "Error: Compile Error: Unexpected token '<'. at line 14 column 13" which I believe is referencing the line, 'List<Task> taskList = [SELECT Task_Created_and_Completed_By_Curent_AE__c FROM Task WHERECreatedById IN :idAcctOwnerSet AND Task_Created_and_Completed_By_Curent_AE__c = true];' 

David
Abdul KhatriAbdul Khatri
I don't see any issue with the code. Did you copy and pasted the code or else?
David Wright 97David Wright 97
All working now. 

Thank you for your time and communication.  

David