Lead time error for change request does not show up for Start Date if it is current time , runs only when start date is few minutes ahead of now time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â07-03-2022 01:10 AM
Hi,
I want to allow users to select start date after 15 minutes from now. I have written below script but cannot achieve this. Please suggest!!
The issue is coming with the lead time error but whenever start date is getting selected as current time , the lead time error does not run, but if start date is 2 to 3minutes ahead of current time, the lead time error shows up.
Should I change the script for lead time or for start date?
Ideally lead time should run if change request is raised with start date within 2 days from now. so it ultimately considers today's date time as well but not behaving as such.
Below are respective scripts:
Planned start date cannot be past or current time (However it is taking current time but runs if there start date is ahead of few seconds from current):
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (g_form.getValue('type') != 'emergency') {
var ajax = new GlideAjax('SCMyDateTimeAjax');
ajax.addParam('sysparm_name', 'nowDateTime');
ajax.addParam('sysparm_startdate', g_form.getValue('start_date'));
ajax.getXML(checkdate);
}
function checkdate(response) {
var currentDate = response.responseXML.documentElement.getAttribute("answer");
if (currentDate == "WrongDate") {
g_form.clearValue('start_date');
g_form.showFieldMsg('start_date', 'date cannot be set in past', 'error');
//alert (g_form.getValue('start_date'));
} else {
g_form.hideFieldMsg('start_date');
}
}
}
Planned start date (Script Include for above):
var SCMyDateTimeAjax = Class.create();
SCMyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
nowDateTime: function() {
var startDate = this.getParameter('sysparm_startdate');
var time = new GlideDateTime();
var currenDate = time.getDisplayValue();
var date2 = startDate.getByFormat('yyyy/MM/dd');
var a = gs.dateDiff(currenDate, startDate, true);
//if(startDate < currenDate){
if (a <= 0) {
return "WrongDate";
}
},
type: 'SCMyDateTimeAjax'
});
Script for Lead Time:(Business Rule Insert/Update):
Condition: when states changes to assess
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var startDate = current.start_date;
var currentDate = new GlideDateTime();
var dttype = 'day';
var risk1 = current.getValue('risk');
var type = current.getValue('type');
var link = (not to be disclosed);
var assess = new SC_ChangeDateTime().getDateTimeDiff(currentDate, startDate, dttype);
var roundoff1 = Math.ceil(assess);
if(parseInt(roundoff1)>0)
{
if (roundoff1 <= 10 && risk1 == '2') {
gs.addInfoMessage(gs.getMessage("The Planned Start Date does not meet the submission lead time required for the specified risk level.") + ' ' + link + ' ');
current.setAbortAction(true);
} else if (roundoff1 <= 5 && risk1 == '3') {
gs.addInfoMessage(gs.getMessage("The Planned Start Date does not meet the submission lead time required for the specified risk level.") + ' ' + link + ' ');
current.setAbortAction(true);
} else if (roundoff1 <= 2 && risk1 == '4') {
gs.addInfoMessage(gs.getMessage("The Planned Start Date does not meet the submission lead time required for the specified risk level.") + ' ' + link + ' ');
current.setAbortAction(true);
}else{
current.setAbortAction(false);
}
}
})(current, previous);
Script Include for above lead time: SC_ChangeDateTime()
var SC_ChangeDateTime = Class.create();
SC_ChangeDateTime.prototype = {
initialize: function() {},
getDateTimeDiff: function(firstDT, secondDT, type) {
var diffTYPE = 'day';
var diff = gs.dateDiff(firstDT, secondDT, true);
var timediff = this._calcDateDiff(diffTYPE, diff);
//return "getDateTimeDiff: FIRST DT: " + firstDT + " -SECOND DT: " + secondDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;
return timediff;
},
_calcDateDiff: function(diffTYPE, seconds) {
var thisdiff;
if (diffTYPE == "day") {
thisdiff = seconds / 86400;
} else if (diffTYPE == "hour") {
thisdiff = seconds / 3600;
} else if (diffTYPE == "minute") {
thisdiff = seconds / 60;
} else if (diffTYPE == "second") {
thisdiff = seconds;
} else {
thisdiff = seconds;
}
return thisdiff;
},
type: 'SC_ChangeDateTime'
};
Issue is that lead time error script runs only when start date is 2 to 3 minutes ahead of now current time. So, we want users to select start date 15 minutes after current time or else it will abort action.
Note: Start date should not be in past too.
Please suggest!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- Labels:
-
Change Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â07-03-2022 08:54 AM
Hi,
check this link if it helps
https://community.servicenow.com/community?id=community_question&sys_id=b9fd6657db2e0910b3c099ead3961994
Mark correct or helpful if it helps
Thanks,
Yousaf
***Mark Correct or Helpful if it helps.***
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â07-03-2022 08:44 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â07-08-2022 07:04 PM
I tried below script and it worked.
Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cdt = g_form.getValue('start_date'); //change the field name as per form
var dttype = 'minute'; //this can be day, hour, minute, second. By default it will return seconds.
if (g_form.getValue('type') === 'normal') {
var ajax = new GlideAjax('script include name');
ajax.addParam('sysparm_name', 'getNowDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);
}
function doSomething(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer < 2880) {
g_form.clearValue('start_date', '');
g_form.showFieldMsg('start_date', 'your message', 'error');
}
}
}
Script Include:
var scriptincludename = Class.create();
testplanstart.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getNowDateTimeDiff: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day
var diff = gs.dateDiff(gs.nowDateTime(), firstDT, true);
var timediff = this._calcDateDiff(diffTYPE, diff);
//return "getNowDateTimeDiff: FIRST DT: " + firstDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;
return timediff;
},
_calcDateDiff: function(diffTYPE, seconds){
var thisdiff;
if (diffTYPE == "day"){thisdiff = seconds/86400;}
else if (diffTYPE == "hour"){thisdiff = seconds/3600;}
else if (diffTYPE == "minute"){thisdiff = seconds/60;}
else if (diffTYPE == "second"){thisdiff = seconds;}
else {thisdiff = seconds;}
return thisdiff;
} ,
type: 'scriptincludename'
});