- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2016 06:36 AM
Dear,
I need to emptied the value of the reference field when user put something wrong like "ggggggg".
What happened is the default filter will use "name starts with <user input>".
This can be change in the sys_properties but rules make that I cannot change global settings.
I tried a client script on chance on the Service Field and it is not working because I cannont get the wrong value that user put.
When user put something wrong, the field goes red, usually end user get the fact that it is wrong. Then they click on the lookup but no lists shown because the filter kept what they wrote wrong.
Thanks in advance.
Regs,
Lô Mouhamadou
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2016 11:25 AM
Hello Mouhamadou,
This is actually a difficult thing to achieve. The newValue as passed into a client script for a reference field will be either the string of the sys_id of the referenced record, or it will be a blank string. The only way to tell whether the user has blanked out the value or entered an invalid value would be to get the actual display control and check. An additional problem is that the onChange script will be called twice when the value is invalid.
These are not insurmountable problems You could use some DOM scripting to work around this, but you must be careful to test thoroughly. Since you will need to do direct DOM manipulation, you should be aware that a customization like this can stop working when the ServiceNow UI is updated. It will be up to you to maintain the functionality if ServiceNow changes the way forms are rendered.
That said, here is an example client script which implements some of the functionality.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
//The new value could be blank because the actual value is blank, or because the user entered an invalid value
if (newValue == '') {
var id = control.id;
id = id.split('.').join('\\.');
id = '#sys_display\\.' + id;
var elem = ($j(id));
var val = elem.val();
//don't run if it's not invalid
if (!elem.hasClass("ref_invalid"))
return;
//it's a legit empty value (should be caught by above check, but just to be safe)
if (!val)
return;
//we might be in the second call
if (window.invalidReferenceValue == val)
return;
//store the value so we can compare on the second time through
window.invalidReferenceValue = val;
setTimeout(function () {
g_form.setValue("cmdb_ci", "");
g_form.showFieldMsg("cmdb_ci", "The Configuration Item you entered does not exist. Please use the magnifying glass to choose from a list of acceptable values", "error");
});
}
}
Again, maintaining a script like this will be your responsibility, since it steps well outside the bounds of the standard g_form API.
Thanks
Cory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2016 09:59 AM
Hi Mouhamadou,
$j("anything") always returns an object- it returns a jQuery collection, even if that collection is empty. So it'll always print out as [object Object].
I am not sure how the field is named on your record producer. The ID may be different, leading to your collection being empty. This is the problem with relying on direct DOM methods- the exact magic you have to use to get a particular control my depend on the sys_id of a variable, or a specific UI version, and might break between instances or upgrades.