You need to sign in to do that
Don't have an account?
TheresaAnderson
Looking for a trigger to create a task from a custom field on an account record
Can anyone share sample code to create a task based on a custom field on an Account record? I believe I'm making my code way too difficult.
I am not 100% sure why it is creating 4 tasks. May be you can put a system.debug just before ltask.add() stmt and check what all tasks are getting added to the list. That way you can check what all is causing the trigger to create 4 tasks. The code snippet above shows the use of oldmap which returns the old object. It prevents the use of two for loops.
All Answers
a.field is the field which would drive the creation of the task. Hope it helps
Thank you for the example - it's exactly what I needed.
Since ISCHANGED is not allowed in a trigger, what are my options?
You will need to check the old and the new value of the field using Trigger.old and Trigger.new
I have one more question - the code is working, but the code is creating 4 tasks.
trigger CreateAlertTaskonAcct on Account (before update) {
List<Task> ltask = new List<Task>();
for(Account o: Trigger.old){
for(Account a: Trigger.new){
if(a.Alert_Status__c=='High' && o.Alert_description__c!=a.Alert_description__c){
Task t = new Task();
t.WhatID=a.Id;
t.Subject='Red Alert';
t.Status='Completed';
t.Priority='Red';
t.Description=a.Alert_Description__c;
t.Project_Comments__c=a.Alert_Description__c;
t.Project_Status_Date__c=a.Alert_Timestamp__c;
t.OwnerId=a.OwnerId;
t.ActivityDate=system.today();
t.RecordTypeId='xxxxxxxxxxxxxxxxxxx';
ltask.add(t);
}
Else if(a.Alert_Status__c=='Medium' && o.Alert_description__c!=a.Alert_description__c){
Task t = new Task();
t.WhatID=a.Id;
t.Subject='Yellow Alert';
t.Status='Completed';
t.Priority='Yellow';
t.Description=a.Alert_Description__c;
t.Project_Comments__c=a.Alert_Description__c;
t.Project_Status_Date__c=a.Alert_Timestamp__c;
t.OwnerId=a.OwnerId;
t.ActivityDate=system.today();
t.RecordTypeId='xxxxxxxxxxxxxxxxxxx';
ltask.add(t);
}
}
}
Insert ltask;
}
I am not 100% sure why it is creating 4 tasks. May be you can put a system.debug just before ltask.add() stmt and check what all tasks are getting added to the list. That way you can check what all is causing the trigger to create 4 tasks. The code snippet above shows the use of oldmap which returns the old object. It prevents the use of two for loops.