- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2024 07:34 AM - edited 08-06-2024 06:02 AM
I have a reference field for the "cmdb_ci" table, within the incident form and I need to get the value of a field that is within this record called "u_safe_box" (it's a check box).
(I'm using client script onChange for this, I tried using var clrID = g_form.getValue('u_safe_box'); )
When a CI is entered that the record has "u_safe_box" as 'true', it has to clear the field and display a message.
How can I do this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2024 07:47 AM
you can get the value using g_form.getReference() with callback method
g_form.getReference('cmdb_ciField', function(ref){
var val = ref.u_safe_box;
alert();
});
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2024 07:41 AM
Hi
Please follow the steps:
1. Write a Client Callable script include and to return the checkbox value.
2. Write an onchange script to call the above script include's method via GlideAjax
Refer: GlideAjax Example Cheat Sheet (UPDATED) - ServiceNow Community
3. based on the returned value, if it returns true, clear the value of the field
Thanks
Deepak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2024 07:47 AM
you can get the value using g_form.getReference() with callback method
g_form.getReference('cmdb_ciField', function(ref){
var val = ref.u_safe_box;
alert();
});
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2024 05:54 AM - edited 08-06-2024 05:57 AM
Thank you all for the solutions!
I tried to do it with "GlideAjax", but it didn't work, it didn't return the value. However, with "g_form.getReference()", it worked.
Code:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var safeBox = g_form.getReference('cmdb_ci', _incHandle); // Reference Field, Callback
function _incHandle(safeBox) {
try {
if (safeBox && String(safeBox).trim() !== '') {
if (safeBox.getValue('u_safe_box') === 'true') {
g_form.clearValue('cmdb_ci');
g_form.addErrorMessage(getMessage('This IC cannot be associated with the call'));
}
}
} catch (err) {
g_form.addErrorMessage('Erro inesperado: ' + err);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2024 09:34 AM
Hi @Calli ,
you can add fields based on your requirement.
Create Script Includes;
Name: commonUtils
Client callable: true (checked)
Script:
var commonUtils = Class.create(); commonUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, { getUserInfo: function(){ var user = this.getParameter('sysparm_user'); var gr = new GlideRecord("sys_user"); gr.get(user); var deptname = gr.department.getDisplayValue(); var dept = gr.getValue('department'); var manager = gr.getValue('manager'); var managername = gr.manager.getDisplayValue(); var location = gr.getValue('location'); var locationname = gr.location.getDisplayValue(); var response = {}; response.deptname = deptname; response.dept = dept; response.manager = manager; response.managername = managername; response.location = location; response.locationname = locationname; return JSON.stringify(response); }, type: 'commonUtils' });
Client Script:
function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; } // Call the GA function var ga = new GlideAjax('commonUtils'); ga.addParam('sysparm_name', "getUserInfo"); ga.addParam('sysparm_user', newValue); //Onchange field is reference field to sys_user table ga.getXMLAnswer(function(answer){ var response = JSON.parse(answer); g_form.setValue('department_name', response.deptname); //used for string field g_form.setValue('department', response.dept); //used for reference field to department table g_form.setValue('manager_name', response.managername); //used for string field g_form.setValue('manager', response.manager); //used for reference field to user table g_form.setValue('location_name', response.locationname); //used for string field g_form.setValue('location', response.location); //used for reference field to location table }); }
Mark the comment as a correct answer and also helpful once worked.
Thanks & Regards,
Sumanth Meda