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
Andrew PerezAndrew Perez 

Apex Trigger: create event from a task

I'm trying to create an event based on a field from a task. I have a workflow in place that will change a task field to Completed once it gets in a certain date range which will then trigger a new event.  I want this new event to copy all the data from the task to the new event.

Here is what I have, doesn't seem to be working:

trigger createEvent on Task (after insert) {   
    List<Event> NewEvent = new List<Event>();
            for (Task newTask: Trigger.New)
               if (newTask.Status == 'Future Completed'){
                NewEvent.add (new Event(
                    OwnerID = newTask.OwnerID,
                    Subject = newTask.Subject,
                    Meeting_Status__c = newTask.Meeting_Status__c,
                    Meeting_Type__c = newTask.Meeting_Type__c,
                    Confirmed_with__c = newTask.Confirmed_with__c,
                    WhatId = newTask.WhatId,
                    WhoId = newTask.WhoId,
                    Priority__c = newTask.Priority__c,
                    Show_Time_As__c = newTask.Show_Time_As__c,
                    Client_Location_Meeting_Date__c = newTask.Client_Location_Meeting_Date__c,
                    Client_Location_Start_Time__c = newTask.Client_Location_Start_Time__c,
                    Client_Location_End_Time__c = newTask.Client_Location_End_Time__c,
                    Location = newTask.Location__c,
                    Directions_to_the_Meeting_Location__c = newTask.Directions_to_the_Meeting_Location__c,
                    Meeting_Confirmed__c = newTask.Meeting_Confirmed__c,
                    Confirmation_Notes__c = newTask.Confirmation_Notes__c,
                    More_Comments__c = newTask.More_Comments__c,
                    Summary__c = newTask.Summary__c,
                    Next_Steps__c = newTask.Next_Steps__c));
                    }
  insert NewEvent;
}
StephaneTStephaneT
you need to write a  before update trigger and write a query before the for loop to show records with a criteria:name='Future Completed'





Andrew PerezAndrew Perez
I have added a query and changed it to a before update but I am getting this error:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger createEvent caused an unexpected exception, contact your administrator: createEvent: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Duration]: [Duration]: Trigger.createEvent: line 28, column 1


Correct me if i'm wrong, but it looks like it's saying that a required field (Duration) is missing...we don't have that as a required field in our org though.
StephaneTStephaneT
Maybe it is required at a database level, try by adding that field and tell me what happens
Andrew PerezAndrew Perez
Added it and still getting the same error
StephaneTStephaneT
-take off NewEvent.add (new Event(
-make sure you put ; instead of ,
-
and append the a to each new field you want to copy

Should be something like this:

for (Task newTask: Trigger.New)
               if (newTask.Status == 'Future Completed'){
                      event a=newevent();
                      a.Client_Location_Start_Time__c = newTask.Client_Location_Start_Time__c;

                           ....


                NewEvent.add(a);
                 }

Andrew PerezAndrew Perez
Here is what I have now:
trigger createEvent on Task (before update) { 
List<Event> NewEvent = new List<Event>();
    List<Task> task = [SELECT OwnerID,Subject,Meeting_Status__c,Meeting_Type__c,Confirmed_with__c,WhatId,WhoId,Priority__c,Show_Time_As__c,Client_Location_Meeting_Date__c,Client_Location_Start_Time__c,Client_Location_End_Time__c,Location__c,
    Directions_to_the_Meeting_Location__c,Meeting_Confirmed__c,Confirmation_Notes__c,More_Comments__c,Summary__c, Next_Steps__c FROM Task WHERE Status = 'Future Completed'];
             for (Task newTask: Trigger.New)
               if (newTask.Status == 'Future Completed'){
                Event a = newevent();
                    a.OwnerID = newTask.OwnerID;
                    a.Subject = newTask.Subject;
                    a.Meeting_Status__c = newTask.Meeting_Status__c;
                    a.Meeting_Type__c = newTask.Meeting_Type__c;
                    a.Confirmed_with__c = newTask.Confirmed_with__c;
                    a.WhatId = newTask.WhatId;
                   a.WhoId = newTask.WhoId;
                    a.Priority__c = newTask.Priority__c;
                    a.Show_Time_As__c = newTask.Show_Time_As__c;
                    a.Client_Location_Meeting_Date__c = newTask.Client_Location_Meeting_Date__c;
                    a.Client_Location_Start_Time__c = newTask.Client_Location_Start_Time__c;
                    a.Client_Location_End_Time__c = newTask.Client_Location_End_Time__c;
                    a.Location = newTask.Location__c;
                    a.Directions_to_the_Meeting_Location__c = newTask.Directions_to_the_Meeting_Location__c;
                    a.Meeting_Confirmed__c = newTask.Meeting_Confirmed__c;
                    a.Confirmation_Notes__c = newTask.Confirmation_Notes__c;
                    a.More_Comments__c = newTask.More_Comments__c;
                    a.Summary__c = newTask.Summary__c;
                    a.Next_Steps__c = newTask.Next_Steps__c;
                   NewEvent.add(a);  }   
}


Getting this error: Error: Compile Error: Method does not exist or incorrect signature: newevent() at line 7 column 27