Insert Multiple Records using Business Rule

michaelcory
Giga Expert

A training class table is updated routinely by Training Admins adding Classes.   I need to find a way to allow them to create "recurring classes".   On the table there is a start date & end date field. Both are date/time fields.  

I've set out by creating a "recurring"(u_recurring) field (true/false) and a "number of occurrences" (u_number of occurrences) field (integer) on the Training Class form.   The idea is that the trainer will set the "recurring" check box to true and then set the "number of occurrences" and the Business Rule will create multiple records based on "number of occurrences" entered.  

Example:   Trainer creates a new record with the following Start & End Dates.   Training checks "recurring" and sets the "Number of Occurrences" to 3.    

Start Date:   2017-06-20 08:00:00

End Date:     2017-06-20 10:00:00

On Submit, create 3 additional records with Start & End Dates 7 days apart

Start Date:   2017-06-27 08:00:00   |   End Date:     2017-06-27 10:00:00

Start Date:   2017-07-04 08:00:00   |   End Date:     2017-07-04 10:00:00

Start Date:   2017-07-11 08:00:00   |   End Date:     2017-07-11 10:00:00

I'm struggling with how to tell the Business Rule to create the "number of occurrences"   and how to set the Start & End dates.

Name: Recurring Class

Table: u_training_class

Insert: true

When: after

Condition: current.u_recurring = true

Script:

(function executeRule(current, previous /*null when async*/) {

  var tc = new GlideRecord('u_training_class');

  tc.newRecord();

  tc.u_start_date = current.getValue('u_start_date') +7;

  tc.u_end_date = current.getValue('u_end_date') +7;

  tc.insert();

})(current, previous);

1 ACCEPTED SOLUTION

why in your case it making duplicate, but you can avoid the duplication by checking this way



var gdt = new GlideDateTime(current.getValue('u_start_date'));


  var gdt2 = new GlideDateTime(current.getValue('u_end_date'));



  var occ = current.u_number_of_occurrences;


  var counter = 1;


  var duration = 7;



  while (counter < occ){


  gdt.addDays(duration);


startDate = gdt.getDisplayValue();


gdt2.addDays(duration);


endDate   = gdt2.getDisplayValue();



var gr = new GlideRecord('u_training_class');


gr.addQuery('u_class_name', current.getValue('u_class_name'));


gr.addQuery('u_start_date',startDate);


gr.addQuery('u_end_date',endDate);


gr.query();


if(!gr.hasNext()){


var tc = new GlideRecord('u_training_class');


  tc.initialize();


  tc.u_class_name = current.getValue('u_class_name');


  tc.u_description = current.getValue('u_description');


  tc.u_attendee_type = current.getValue('u_attendee_type');


  tc.u_training_room = current.getValue('u_training_room');


tc.u_start_date = startDate;


tc.u_end_date = endDate;


  tc.insert();


}


  counter++;


  duration += 7;


  }


View solution in original post

17 REPLIES 17

Hi Balaji,



I sent the attached email to the Community in the hope that they can mark the answer as correct.



Thank you,


Below is my understanding of why the "Mark as Correct" button does not display.   In the second step, I forgot to "Mark this discussion as a question".  



  • Open a new Discussion thread

find_real_file.png


  • Mark discussion as a question

find_real_file.png


BALAJI40
Mega Sage

Date validation should be,



var gdt = new GlideDateTime(current.getValue('start_date');


gdt.addDays(7);


tc.u_start_date = gdt.getDisplayValue();



var gdt2 = new GlideDateTime(current.getValue('end_date');


gdt2.addDays(7);


tc.u_end_date = gdt2.getDisplayValue();