You need to sign in to do that
Don't have an account?
David 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;
}
}
}
}
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;
}
}
}
}
All Answers
Best regards
David
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
Appreciate your help.
Is it possible to embed this additional text into your original script?
Best regards
David
- 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.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
Thank you for your time and communication.
David