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

sachin_namjoshi
Kilo Patron
Kilo Patron

You need use store value of "number of occurrences"   in variable in your business rule and then use counter to match number of occurrences.


Also, you need to use OOB GlideDateObject method to add days.



var gdt = current.tc.u_start_date.getGlideObject();  


tc.u_start_date=gdt.addDays(7)



You could use below script



var occ = current.u_number of occurrence;


var counter = 1;


while ( counter <= occ){



  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();


counter ++;



}


Thank you for the code.   The below script works, but it's adding an additional duplicate start/end time record.   Any way to avoid that and only have 3 records generated if number of occurrences = 3?


find_real_file.png



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



  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;


  while (counter <= occ){



  var tc = new GlideRecord('u_training_class');


  tc.newRecord();


  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(7);


  tc.u_end_date   = gdt2.getDisplayValue();


  gdt2.addDays(7);


  tc.insert();


  counter++;


  }


})(current, previous);


Please check the iterations of while loop through gs.log(count) or rub the same script in background script and see how many records its showing.


Log shows Occurrence = 3 and Record Count is 3.   Appears that it's counting after inserting a record.   I have the BR set to run on Insert.   Should it be set to run on Update only?   Would the BR still run if Update only?



find_real_file.png