- 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 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 10:20 AM
Great thank you Jamie this works great! The only thing i'm noticing is it does allow my to enter days in the duration field. I'm trying to hide the 'Days' part of the duration field and I can do that but can't hide the label using DOM or jquery. Anyways, thanks for the solution and I'll see if I can prohibit Days' from being entered somehow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2022 08:33 AM