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

Sorry Mike again i typed it as wrong, remove equal to operator


while (counter < occ)


Almost there.   Now getting only 3 records, but still a duplicate


find_real_file.png


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;


  }


That did it Balaji.   Thank you for your time and assistance on this.   Much appreciated!   I just realized that I must not have marked this discussion as a Question, so I don't have the "Correct Answer" link.   My apologies for that.   I don't know of a way to mark discussion as a question after it's been submitted.   I will mark your solution and Sachin's input as helpful and like.   Thanks again to you both for your help.


Please welcome Mike.


If you are viewing this from the community inbox you will not see the correct answer button.   If so, please review How to Mark Answers Correct From Inbox View


How To Mark Answers Correct From Community Inbox