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

Upon further review, the ideal outcome would be the following:



Trainer fills out a new Class record with the following Start/End dates and selects 3 for the number of occurrences


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



On Submit, the BR runs and creates 2 additional records with Start & End Dates 7 days ahead of the submitted record


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



The end result as the only 3 records are generated if the Trainer selects 3 as the number of occurrences.


Please modify your business rule as follows:



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){



  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 = gdt.getDisplayValue();


  gdt.addDays(duration);


  tc.u_end_date   = gdt2.getDisplayValue();


  gdt2.addDays(duration);


  tc.insert();


  counter++;


  duration += 7;


  }


Thank you Sachin.   I applied the script but still getting the duplicate Class record on insert.   This solution would be perfect if not for the duplicate Class record created. Is there a way to script the counter to only add 2 records if the "number of occurrences" is 3 and so on?   That way, we would get the inserted record and only 2 more.  


Or if the 1st record could be 7 days from the inserted record.   Then we would get 4 records, but the dates would all be 7 days apart.   Then 3 occurrences would mean the submitted record + 3 more.  



find_real_file.png


If you want to insert 2 records means change the while loop as,



while (counter <=occ){



Try with this, please check and let me know, if you have any issue again.


Hi balaji,


The while loop is already set to while (counter <= occ) and is inserting 3 records.   What do I need to change it to so that I only get 2 records inserted?