Dates Validation using Client scripts and Data Policy

Lucky1
Tera Guru

Hello all,

 

On a change request, I want to make sure the difference between Start date and End date to be at least 15 mins but not below that.

It should be applicable during a change request record creation using Integration also.

So, I guess Data policy will work.

So, can someone please give me the code for this scenario.

 

 

Regards,

Lucky

1 ACCEPTED SOLUTION

Nishant8
Giga Sage

Hello @Lucky1 , You can write a Business Rule - configured to run before Insert and Update. you can use below script

 

(function executeRule(current, previous /*null when async*/) {
var startDate = current.work_start;
var endDate = current.work_end;
var startGDT = startDate.getGlideObject();
var endGDT = endDate.getGlideObject();
var compareTime = 900000; // 15 mins milliseconds
var duration = GlideDateTime.subtract(startGDT, endGDT).getNumericValue();
if (duration < compareTime) {
gs.addErrorMessage("Date difference should be more than 15 mins");
}
})(current, previous);

 

Regards,

Nishant

View solution in original post

14 REPLIES 14

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Lucky1 

 

I tried UI policy but issue is it has only hours

 

AGLearnNGrow_0-1739781450762.png

 

 

Try to put .25 hours and let see if it work with no code way.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Vishwa Pandya19
Mega Sage

Hello,

 

Check out below community links, they might be able to help you:

https://www.servicenow.com/community/itsm-forum/allow-user-to-select-date-if-is-more-than-15minutes/...

https://www.servicenow.com/community/developer-forum/find-difference-between-two-dates-in-minutes/m-...

If my answer has helped you in any way please mark it correct or helpful.

 

maheshkhatal
Mega Sage

@Lucky1 You can create a client script as given below:

Client Script Details, you can adjust it as per your requirement for the subsequent action to be taken:

  • Type: onChange

  • Table: Change Request (change_request)

  • Field: Start date (start_date) or End date (end_date)

  • Script: The script will validate the difference between the two dates when either field is updated.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    // Ensure the script runs only when the form is not loading
    if (isLoading || newValue === '') {
        return;
    }

    // Get the values of Start date and End date fields
    var startDate = g_form.getValue('start_date');
    var endDate = g_form.getValue('end_date');

    // If either field is empty, exit the script
    if (!startDate || !endDate) {
        return;
    }

    // Convert the date strings to JavaScript Date objects
    var startDateObj = new Date(startDate);
    var endDateObj = new Date(endDate);

    // Calculate the difference in milliseconds
    var timeDifference = endDateObj - startDateObj;

    // Convert the difference to minutes
    var timeDifferenceInMinutes = timeDifference / (1000 * 60);

    // Check if the difference is less than 15 minutes
    if (timeDifferenceInMinutes < 15) {
        // Show an error message to the user
        g_form.showFieldMsg('end_date', 'The difference between Start date and End date must be at least 15 minutes.', 'error');

        // Clear the End date field to force the user to correct it
        g_form.setValue('end_date', '');
    } else {
        // Clear any existing error messages
        g_form.hideFieldMsg('end_date');
    }
}

 

If my response helped you in anyway please mark it as helpful.

Thank you,

Mahesh.

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Lucky1 

you cannot use data policy as it's not possible to use 15mins.

Better you can use the logic during the integration itself.

What type of integration is being used? Import set or Scripted REST API?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader