- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 06-03-2024 03:41 AM
Hi All,
In this article, we will go through one of the ways of creating multiple records from a record producer based on selected week days.
Use Case:
There is a record producer having 2 'date/time' type field named 'Actual Start Date' and 'Actual End Date' and one 'date' type field named 'Recurring Date'. There are 7 checkbox fields which represents 7 days of the
week(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday). If same date is selected in Actual start date and actual end date but the time will be different and there will be a future date in the Recurring date then it will create the records based on the days of week selected in between the start/end date and recurring date.
Example: Let's understand this with an example:
Consider 'Actual Start Date' is selected as '2024-05-06 07:40:59' and Actual End Date is selected as '2024-05-06 17:40:59'. 'Recurring Date' is selected as '2024-05-15' and day of week is selected - (Monday, Wednesday and Friday) .
So, it means that when user will submit this form there should be 5 records getting created with the below date/time:
Record No.(Imaginary) | Actual Start Date | Actual End Date | Day of Week |
INC0000238 | 06/05/2024 07:40:59 | 06/05/2024 17:40:59 | Monday |
INC0000239 | 08/05/2024 07:40:59 | 08/05/2024 17:40:59 | Wednesday |
INC0000240 | 10/05/2024 07:40:59 | 10/05/2024 17:40:59 | Friday |
INC0000241 | 13/05/2024 07:40:59 | 13/05/2024 17:40:59 | Monday |
INC0000242 | 15/05/2024 07:40:59 | 15/05/2024 17:40:59 | Wednesday |
Implementation Steps:
In order to achieve the above, the 'script' part of the record producer will be as below:
current.setAbortAction(true); //To prevent the submission of record with the selected date/time(Initial record)
var gdt= new GlideDateTime(producer.actual_start_timestamp);
var gdt1= gdt.getDate();
var gdtEnd= new GlideDateTime(producer.actual_end_timestamp);
var repDT = new GlideDateTime(producer.repeat_date);
// Creating an empty array to store days
var mondays = [];
var tuesdays = [];
var wednesdays = [];
var thursdays = [];
var fridays = [];
var saturdays = [];
var sundays = [];
for (var date_app = gdt; date_app.compareTo(repDT) <= 0; date_app.addDays(1)) {
if (date_app.getDayOfWeek() == 1 && producer.monday == 'true') {
mondays.push(new GlideDateTime(date_app));
}
if (date_app.getDayOfWeek() == 2 && producer.tuesday == 'true') {
tuesdays.push(new GlideDateTime(date_app));
}
if (date_app.getDayOfWeek() == 3 && producer.wednesday == 'true') {
wednesdays.push(new GlideDateTime(date_app));
}
if (date_app.getDayOfWeek() == 4 && producer.thursday == 'true') {
thursdays.push(new GlideDateTime(date_app));
}
if (date_app.getDayOfWeek() == 5 && producer.friday == 'true') {
fridays.push(new GlideDateTime(date_app));
}
if (date_app.getDayOfWeek() == 6 && producer.saturday == 'true') {
saturdays.push(new GlideDateTime(date_app));
}
if (date_app.getDayOfWeek() == 7 && producer.sunday == 'true') {
sundays.push(new GlideDateTime(date_app));
}
}
//var i = 0 > it created record for the selected date as well
for (var i = 0; i < mondays.length; i++) {
var fixStamp = mondays[i].getValue().split(' ');
var fixedDate = fixStamp[0]; //Extracted date
var getTimeOf = gdtEnd.getValue().split(' ');
var exactTime = getTimeOf[1]; //Extracted Time
var finalEndDt = fixedDate + ' ' + exactTime;
var passGDT = new GlideDateTime(finalEndDt); //Final End date
var inc= new GlideRecord('incident');
inc.initialize();
inc.u_repeat_till = producer.repeat_date.getDisplayValue();
inc.u_start_timestamp = mondays[i];
inc.u_end_timestamp = passGDT;
inc.update();
}
for (var j = 0; j < tuesdays.length; j++) {
var fixTueStamp = tuesdays[j].getValue().split(' ');
var fixedTueDate = fixTueStamp[0]; //Extracted date
var getTueTimeOf = gdtEnd.getValue().split(' ');
var exactTueTime = getTueTimeOf[1]; //Extracted Time
var finalTueEndDt = fixedTueDate + ' ' + exactTueTime;
var passTueGDT = new GlideDateTime(finalTueEndDt); //Final End date
var incTue= new GlideRecord('incident');
incTue.initialize();
incTue.u_start_timestamp = tuesdays[j];
incTue.u_end_timestamp = passTueGDT;
incTue.update();
}
for (var k = 0; k < wednesdays.length; k++) {
var fixWedStamp = wednesdays[k].getValue().split(' ');
var fixedWedDate = fixWedStamp[0]; //Extracted date
var getWedTimeOf = gdtEnd.getValue().split(' ');
var exactWedTime = getWedTimeOf[1]; //Extracted Time
var finalWedEndDt = fixedWedDate + ' ' + exactWedTime;
var passWedGDT = new GlideDateTime(finalWedEndDt); //Final End date
var incWed = new GlideRecord('incident');
incWed.initialize();
incWed.u_start_timestamp = wednesdays[k];
incWed.u_end_timestamp = passWedGDT;
incWed.update();
}
for (var thru = 0; thru < thursdays.length; thru++) {
var fixThrStamp = thursdays[thru].getValue().split(' ');
var fixedThrDate = fixThrStamp[0]; //Extracted date
var getThrTimeOf = gdtEnd.getValue().split(' ');
var exactThrTime = getThrTimeOf[1]; //Extracted Time
var finalThrEndDt = fixedThrDate + ' ' + exactThrTime;
var passThrGDT = new GlideDateTime(finalThrEndDt); //Final End date
var incThr = new GlideRecord('incident');
incThr.initialize();
incThr.u_start_timestamp = thursdays[thru];
incThr.u_end_timestamp = passThrGDT;
incThr.update();
}
for (var fri = 0; fri < fridays.length; fri++) {
var fixFriStamp = fridays[fri].getValue().split(' ');
var fixedFriDate = fixFriStamp[0]; //Extracted date
var getFriTimeOf = gdtEnd.getValue().split(' ');
var exactFriTime = getFriTimeOf[1]; //Extracted Time
var finalFriEndDt = fixedFriDate + ' ' + exactFriTime;
var passFriGDT = new GlideDateTime(finalFriEndDt); //Final End date
var incFri = new GlideRecord('incident');
incFri.initialize();
incFri.u_start_timestamp = fridays[fri];
incFri.u_end_timestamp = passFriGDT;
incFri.update();
}
for (var sat = 0; sat < saturdays.length; sat++) {
var fixSatStamp = saturdays[sat].getValue().split(' ');
var fixedSatDate = fixSatStamp[0]; //Extracted date
var getSatTimeOf = gdtEnd.getValue().split(' ');
var exactSatTime = getSatTimeOf[1]; //Extracted Time
var finalSatEndDt = fixedSatDate + ' ' + exactSatTime;
var passSatGDT = new GlideDateTime(finalSatEndDt); //Final End date
var incSat = new GlideRecord('incident');
incSat.initialize();
incSat.u_start_timestamp = saturdays[sat];
incSat.u_end_timestamp = passSatGDT;
incSat.update();
}
for (var sun = 0; sun < sundays.length; sun++) {
var fixSunStamp = sundays[sun].getValue().split(' ');
var fixedSunDate = fixSunStamp[0]; //Extracted date
var getSunTimeOf = gdtEnd.getValue().split(' ');
var exactSunTime = getSunTimeOf[1]; //Extracted Time
var finalSunEndDt = fixedSunDate + ' ' + exactSunTime;
var passSunGDT = new GlideDateTime(finalSunEndDt); //Final End date
var incSun = new GlideRecord('incident');
incSun.initialize();
incSun.u_start_timestamp = sundays[sun];
incSun.u_end_timestamp = passSunGDT;
incSun.update();
}
}
If this helped you in any way, Please hit like or mark it as helpful. Thanks.
- 612 Views