SN_Learn
Kilo Patron
Kilo Patron

Hi All,

 

In this article, we will go through one of the ways of creating multiple records from a record producer based on certain condition.

 

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'.  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 creating that many records in our table on which the record producer is setup.

 

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-09'. So, it means that when user will submit this form there should be 4 records getting created with the below date/time:

 

Record No.(Imaginary) Actual Start Date Actual End Date
INC0000234 06/05/2024 07:40:59 06/05/2024 17:40:59
INC0000235 07/05/2024 07:40:59 07/05/2024 17:40:59
INC0000236 08/05/2024 07:40:59 08/05/2024 17:40:59
INC0000237 09/05/2024 07:40:59 09/05/2024 17:40:59

 

 

The 'Actual Start Date' and 'Actual End Date'  have been restricted to only select the same date with the help of onChange catalog client script.

 

 

Implementation Steps:

 

In the 'script' part of the record producer write the below script:

 

   var gd = new GlideDateTime(producer.actual_start_timestamp);
    var date = gd.getDate().getDisplayValue();

    var gd1 = new GlideDateTime(producer.actual_end_timestamp);
    var date1 = gd1.getDate().getDisplayValue();

    var repUntil = new GlideDate();
    repUntil.setValue(producer.repeat_date);
    var repeatUnt = repUntil.getDisplayValue();

    var getNumOfDays = gs.dateDiff(date1, repeatUnt);
    var daysNos = parseInt(getNumOfDays);
    var count = 0;

    for (var d = 0; d < daysNos; d++) {
        count++;
        var depStart = new GlideDateTime(producer.actual_start_timestamp);
        depStart.addDays(count);
        var depEnd = new GlideDateTime(producer.actual_end_timestamp);
        depEnd.addDays(count);
        var incidentRec = new GlideRecord('incident');
        incidentRec.initialize();
        incidentRec.u_start_timestamp = depStart.getDisplayValue();
        incidentRec.u_end_timestamp = depEnd.getDisplayValue();
        incidentRec.u_repeat_till = producer.repeat_date.getDisplayValue();
        incidentRec.insert();
    }

 

This will now create multiple records(as discussed above) once submitted. That's all for this article.

 

If this helped you in any way, Please hit like or mark it as helpful. Thanks.

Version history
Last update:
‎05-16-2024 05:19 AM
Updated by:
Contributors