1

I'm working on functionality to create Activity__c records in bulk when a PlanSchedule__c record is created. I am able to insert X number of Activity__c records based on a calculated value derived from subtracting the start date from the end date then dividing by the specified frequency, which is either 180 or 365 days. What I cannot figure out is how to specify the Scheduled_Date__c for each inserted Activity__c record. The first Activity__c record should have a Scheduled_Date__c equal to the First_Scheduled_Date__c specified in the PlanSchedule__c record, which is achieved in the existing code. Each subsequent Activity__c record should have a Scheduled_Date__c equal the prior record's Scheduled_Date__c plus 180 or 365 days.

For example:

  • Activity1: Scheduled_Date__c = First_Scheduled_Date__c
  • Activity2: Scheduled_Date__c = Activity1's Scheduled_Date__c + Frequency__c
  • Activity3: Scheduled_Date__c = Activity2's Scheduled_Date__c + Frequency__c

How can the code below be modified to achieve this?

The trigger calls the below class "after insert":

public class PlanScheduleCreateActivities {

public static void schedule(List<PlanSchedule__c> schedules) {

    List<Activity__c> activities = new List<Activity__c>();

    // For each schedule processed by the trigger, add a new  
    // activity record for the specified activity type. 

    for (PlanSchedule__c newSchedule : schedules) {
        if(newSchedule.Activity__c != null) {
            for(Integer i = 0; i < newSchedule.Quantity_Calculated__c; i++) {
                activities.add(new Activity__c(
                    Clinical_Study__c = newSchedule.Clinical_Study__c,
                    Activity__c = newSchedule.Activity__c,
                    Vendor__c = newSchedule.Vendor__c,
                    Schedule__c = newSchedule.Id,
                    Assigned_to__c = newSchedule.Assigned_to__c,
                    Scheduled_Date__c =newSchedule.First_Scheduled_Date__c)
                );
            }
        }
    }

    insert activities;        
}
}
2

Tyler,

you can simply add frequency * (i+1) to the first schedule date for each subsequent activity. something like this

          int frequency = 180;// or 365

          for(Integer i = 0; i < newSchedule.Quantity_Calculated__c; i++) {
                activities.add(new Activity__c(
                    Clinical_Study__c = newSchedule.Clinical_Study__c,
                    Activity__c = newSchedule.Activity__c,
                    Vendor__c = newSchedule.Vendor__c,
                    Schedule__c = newSchedule.Id,
                    Assigned_to__c = newSchedule.Assigned_to__c,
                    Scheduled_Date__c = newSchedule.First_Scheduled_Date__c)  + (frequency * i)
                );
            }
1
  • Thank you. I got it to work with integer frequency = integer.valueOf(newSchedule.Frequency_Calculated__c); and Scheduled_Date__c = newSchedule.First_Scheduled_Date__c + (frequency * i)
    – Tyler
    Jan 22 '15 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.