in ootb we use this script in servicenow, how to do the same in ritm form, i see there is a client script, business rule and one sys_ui_style record for this,
function onChange(control, oldValue, newValue, isLoading) {
var callerLabel = (′label.incident.cal('sys_display.incident.caller_id');
if (!callerLabel || !callerField)
return;
Assistant
User
showing this error below field
onChange script error: TypeError: calledField.setStyle is not a function function () { [native code] }
function vipCallerCallback(caller) {
var callerLabel = (′label.incident.callerid′).down(′label′);varcallerField=('sys_display.incident.caller_id');
if (!callerLabel || !callerField)
return;
code
//check for VIP status
if (caller.vip == 'true') {
var bgPosition = "95% 55%";
if (document.documentElement.getAttribute('data-doctype') == 'true')
bgPosition = "5% 45%";
callerLabel.setStyle({backgroundImage: "url(images/icons/vip.gif)", backgroundRepeat: "no-repeat", backgroundPosition: bgPosition, paddingLeft: '30px' });
callerField.setStyle({color: "red"});
} else {
callerLabel.setStyle({backgroundImage: ""});
callerField.setStyle({color: ""});
}
}
To adapt the provided script to work on the Requested Item (RITM) form in ServiceNow, we need to make a few modifications, particularly in referencing the appropriate field and form element IDs, as well as handling the specific configuration for the requested item records (sc_req_item table).
Here's how you can do it:
Client Script: Adapt the client script to the RITM form.
javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var callerLabel = $('label.sc_req_item.your_caller_field'); // Replace 'your_caller_field' with your caller field name in RITM form
var callerField = $('sys_display.sc_req_item.your_caller_field'); // Replace 'your_caller_field' with your caller field name in RITM form
if (!callerLabel || !callerField) return;
if (!newValue) {
callerLabel.setStyle({backgroundImage: ""});
callerField.setStyle({color: ""});
return;
}
g_form.getReference('your_caller_field', vipCallerCallback); // Replace 'your_caller_field' with your caller field name
}
function vipCallerCallback(caller) {
var callerLabel = $('label.sc_req_item.your_caller_field').down('label'); // Replace 'your_caller_field' with your caller field name in RITM form
var callerField = $('sys_display.sc_req_item.your_caller_field'); // Replace 'your_caller_field' with your caller field name in RITM form
if (!callerLabel || !callerField) return;
// Check for VIP status
if (caller.vip == 'true') {
var bgPosition = "95% 55%";
if (document.documentElement.getAttribute('data-doctype') == 'true')
bgPosition = "5% 45%";
callerLabel.setStyle({
backgroundImage: "url(images/icons/vip.gif)",
backgroundRepeat: "no-repeat",
backgroundPosition: bgPosition,
paddingLeft: '30px'
});
callerField.setStyle({color: "red"});
} else {
callerLabel.setStyle({backgroundImage: ""});
callerField.setStyle({color: ""});
}
}
In these script snippets, replace 'your_caller_field' with the actual field name you want to reference on the RITM form.
Business Rules:
Ensure any business rules relevant to VIP callers are also applicable to RITM if needed. Sometimes no additional business rules are necessary if the data is consistent and correctly fetched via client script.
sys_ui_style Entry:
You might not need to create or modify sys_ui_style entries for this case if your client script handles the style dynamically. However, if you have styles you want to persist on the form, you could still use sys_ui_style.
To summarize, the key is to modify the IDs and field names to match those on the sc_req_item form. Use these updated scripts and check all elements manually to ensure they fit in the context of RITM forms.
If you encounter specific field IDs or configurations that don't correspond, you can inspect the RITM form elements via browser developer tools to accurately identify the IDs and names needed for your scripts.
The error onChange script error: TypeError: calledField.setStyle is not a function suggests that the callerField variable is not being properly initialized to the expected DOM element. In ServiceNow's GlideForm API, you typically interact with form fields using methods provided by the g_form object rather than directly manipulating the DOM elements.
Instead of directly accessing the DOM elements using the $ function, you should use g_form methods to accomplish your tasks. Here is how you can adapt your script to use g_form methods correctly:
Client Script: Update the client script to interact with form fields using g_form methods.
javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
if (!newValue) {
g_form.resetLabel('your_caller_field'); // Replace 'your_caller_field' with the actual field name
g_form.setDisplay('your_caller_field', false);
return;
}
g_form.getReference('your_caller_field', vipCallerCallback); // Replace 'your_caller_field' with the actual field name
}
function vipCallerCallback(caller) {
if (caller.vip == 'true') {
var imageURL = "url(images/icons/vip.gif)";
var bgPosition = "95% 55%";
if (document.documentElement.getAttribute('data-doctype') == 'true')
bgPosition = "5% 45%";
var vipLabel = {
backgroundImage: imageURL,
backgroundRepeat: "no-repeat",
backgroundPosition: bgPosition,
paddingLeft: '30px'
};
g_form.setDisplay('your_caller_field', true);
g_form.setStyle('your_caller_field', 'color', 'red');
g_form.setLabelOf('your_caller_field', vipLabel);
} else {
g_form.setLabelOf('your_caller_field', {
backgroundImage: ""
});
g_form.setStyle('your_caller_field', 'color', '');
}
}
In this script:
Replace 'your_caller_field' with the actual name of your caller field in the RITM form.
g_form.resetLabel('your_caller_field') resets any custom style applied to the label.
g_form.setDisplay('your_caller_field', true) ensures the field is displayed if the condition for VIP is met.
g_form.setStyle('your_caller_field', 'color', 'red') sets the text color of the field.
g_form.setLabelOf('your_caller_field', {backgroundImage: imageURL}) sets the background image of the field label when the caller is VIP.
This script uses ServiceNow APIs to modify the appearance and behavior of form fields in a more reliable, supported way, avoiding direct DOM manipulations that can be error-prone. Make sure to test the script thoroughly to ensure it works in the context of your specific RITM form setup.