- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2014 10:00 AM
Hi,
We are using the Time worked field on task and I am trying to write a script which will prevent users from entering time greater than 24 hrs. I want to do this by data validation eessentialy to say: "if(duration > 24hrs") prevent users from submitting record (and pop up an alert). I know this has to be done using GlideAjax because a Client script alone cannot handle calculation.
I created the Client script and Script include like it says in the WIKI but I can't seem to be able to use 'dateNumericValue' to get the value of the duration field in ms.
(Ignore 'Hello world' labels, I just copied the wiki and I'll fix these later.) Right now I'm just trying to return the time_value in milliseconds.
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var val = g_form.getValue('time_worked');
var ga = new GlideAjax('HelloWorld');
ga.addParam('sysparm_name','helloWorld');
ga.addParam('sysparm_user_name', val);
ga.getXML(HelloWorldParse);
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
}
Script Include:
var HelloWorld = Class.create();
HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {
helloWorld: function() {
var val_ms = this.getParameter('sysparm_user_name');
val_ms = val_ms.dateNumericValue();
return "Hello " + val_ms + "!";
},
_privateFunction: function() { // this function is not client callable
}
});
Solved! Go to Solution.
- Labels:
-
Service Mapping
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2014 07:58 AM
Quick and dirty, but I think you get the idea. Actually should be able to just parse the duration field using javascript to achieve the desired result.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
//Parse the duration field and if it comes up as more than 24 hours, show an error
var date_str = newValue.split(' ');
var days = parseInt(date_str[0]);
if (days && date_str[1] != '00:00:00') g_form.showFieldMsg('u_test_duration','Duration is greater than one day','error');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2014 02:27 AM
As per my analysis, i think you need to find out the duration. May be the below link will help you for finding
the duration in seconds not in ms.
DurationCalculator - ServiceNow Wiki
try below script
var dc = new DurationCalculator();
gs.print(dc.calcScheduleDuration("2012-04-10 08:00:00","2012-04-14 06:00:00"));
change like this
gs.print(dc.calcScheduleDuration("pass Created Date" ,"pass Current Date");
The result will be in seconds and convert your 24 hours into seconds and find the difference between two duration.
Hope this will help a little bit.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2014 05:55 AM
Thank you Anish for the reponse but the time_worked field I am using is already a glide_duration field with the format "dd hh:mm:ss". I know dateNumericValue() will change this to milliseconds in a business rule but I am trying to do this in a script include. One thought T have is to call a business rule from the script include but I am unsure if I can or how to do this.
Basically, here's the scenario:
-User enters duration value into Time Worked field on incident (dd hh:mm:ss)
-If the value is greater than 24hrs I want an alert to pop up saying the user must enter a value less than 24hrs.
-Getting the value and comparing to 24hrs can't be done client side that's why I'm using GlideAjax to make a call to server side.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2014 06:36 AM
If you need to do this in a client script, you should just be able to run g_form.getIntValue of the fields in question and then do a comparison on that.
For example, this OOB script on the sysrule_quota table prevents you from having a max duration of less than 5 seconds and more than 2592000 seconds (30 days):
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '')
return;
var maxDuration = g_form.getIntValue('max_duration');
if (maxDuration < 5 || maxDuration > 30 * 24 * 60 * 60) {
g_form.showFieldMsg('max_duration', getMessage('The maximum duration must be between 5 and 2,592,000 seconds (30 days)'), 'error');
}
}
I would suggest this method rather than doing a glideAjax call, if only because it saves you a hit to the server.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2014 07:02 AM
Jamie thank you for your answer but I tried using getIntValue() when I return the value from my duration field I get the integers entered not the number of seconds the duration is. For example, when I put in '2 Days 00 Hours 00 Minutes 00 Seconds' it returns "2000000" , "0 Days 00 Hours 12 Minutes 34 Seconds" it returns "1234" and so on.