Business rule on Date Validation for change requests

Vamshi_ch123
Tera Contributor

Hello All,

 

I'm trying to validate if more than 2 changes have same start date and end end date then restrict creating a third change with same time but i tried using below script it is not working

Could someone please suggest

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

    var startDate = current.start_date;
    var endDate = current.end_date;

    if (!startDate || !endDate) {
        gs.addErrorMessage('Start Date and End Date must be specified.');
        current.setAbortAction(true); 
        return;
    }

    var startDateTime = new GlideDateTime(startDate);
    var endDateTime = new GlideDateTime(endDate);

    gs.debug('Start DateTime: ' + startDateTime.getDisplayValue());
    gs.debug('End DateTime: ' + endDateTime.getDisplayValue());

    function stripSeconds(gdt) {
        var gdtString = gdt.getDisplayValue();
        var strippedString = gdtString.substring(0, 16) + ":00";
        return new GlideDateTime(strippedString);
    }

    startDateTime = stripSeconds(startDateTime);
    endDateTime = stripSeconds(endDateTime);

    gs.debug('Stripped Start DateTime: ' + startDateTime.getDisplayValue());
    gs.debug('Stripped End DateTime: ' + endDateTime.getDisplayValue());

    var gr = new GlideRecord(current.getTableName());
    gr.addQuery('sys_id', '!=', current.sys_id); 
    gr.addQuery('start_date', startDateTime); 
    gr.addQuery('end_date', endDateTime); 
    gr.query();

    gs.debug('Encoded Query: ' + gr.getEncodedQuery());

    var count = 0;
    while (gr.next()) {
        count++;
    }

    gs.debug('Number of overlapping records: ' + count);

    if (count >= 2) {
        gs.addErrorMessage('The selected start and end time slot is already taken by two changes. Please select another time.');
        current.setAbortAction(true); 
    } else {
        gs.debug('Allowed: Less than 2 overlapping records found.');
    }

})(current, previous);

 

Thank you

2 REPLIES 2

ShubhamGarg
Kilo Sage

Hello @Vamshi_ch123 ,

 

Are you trying to use validate these conditions before update of any change request using Business Rule ?

 

To help you further, Can you let me know exact error message you are getting? How many records getting returned in count?

 

Kindly mark this as Accepted Solution/Helpful if it helps.

Regards,

Shubham

Hi @ShubhamGarg 

 

Yes , so far it was not throwing any error just allowing me to create multiple records for same time

Vamshi_ch123_0-1722952011699.png

Thank you