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
Tony QuintanaTony Quintana 

Copy data from custom field in Task activity and paste into a new Task

Hi All,

I'm a newbie to triggers but from everything I've read I need to use one to solve my dilemma. How do i go about writing a trigger that will copy a field from a task and paste that into the same field in a new task? I can's seem to find many examples of teh syntax to copy fields within an object then paste them into a new object.

Thanks
SivaGSivaG
Hi Tony,

Its possible to create a new task. But I need to know the exact requirement like
On which Object this task should be created, which field and when to create a new task i.e. when the task is inserted or updated etc.,

Thanks
Kumar
Tony QuintanaTony Quintana
Hi Kumar,

I’ve created two custom activity fields  - Alert_comment__c  (text)  and auto_renew_days__c (picklist) .  When a user creates a new task they select a subject type and an appropriate due date for that task. There are instances where they would like to extend the due date, so a custom activity field was created labeled 'auto_renew_days__c' which is a picklist that has the option of 0, 30 , 60.  This field has been added to the Task object form. An additional custom field was created labeled 'Alert_comment_c' which is a text box.

I created a workflow that compares the due date to Today() and if it equals today then it checks to see if the 'auto_renew_days__c" is 30, if it is then it creates a new task and updates the due date + 30 days and reset the auto_renew_days__c back to 0 (the 60 day workflow works the same way).  I'd like to be able to copy the 'Alert_comment__c' field from the previous task into the newly created task but cant seem to figure out how to do it. Another criteria to keep in mind is the Subject field within the Task Object must be equal to 'Alert' if not no workflows or triggers need to be run.

Thanks,
Tony
SivaGSivaG
Hi Tony,

You should not create a new task in Workflow rule. The below trigger does that. Just do a field update(due date and auto_renew_days__c)  in your workflow.

trigger TaskTrigger on Task (after update) {
  List<Task> TaskInsList = new List<Task>();
  for(Task t : Trigger.New){
    if(t.auto_renew_days__c == 0 && t.auto_renew_days__c != Trigger.oldMap.get(t.Id).auto_renew_days__c){
       TaskInsList.add(t);
    }
  }
  if(!TaskInsList.isEmpty()){
    try{
        insert TaskInsList;
    }
    Catch(DMLException de){
       System.debug('Exception Occurred on insert of task object: '+ de);
   }
  }
}

PS: Please mark this as Best Answer, if this answer solves your problem.
Tony QuintanaTony Quintana
Hi Kumar,

I'm getting the following error:
Error: Compile Error: Comparison arguments must be compatible types: String, Integer at line 5 column 8

The auto_renew_day__c is a picklist field with three options: 0, 30, 60.
SivaGSivaG
Hi Tony,

Replace t.auto_renew_days__c == 0 with t.auto_renew_days__c == '0'

Thanks
Kumar
Tony QuintanaTony Quintana
A new task is not being created and therefore the comments are not being copied over..