Category:
Updated on: August 30, 2025  |  0

Client Script Patterns 1 - OnLoad and OnChange

đź”§Client Script Patterns:

Examples & Scenarios

 

1) onLoad: prepare the form

1.1 Hide fields for non-admins

 
function onLoad() { if (!g_user.hasRole('admin')) { g_form.setDisplay('vip', false); g_form.setReadOnly('priority', true); } }

1.2 Show a one-time banner message

 
function onLoad() { g_form.addInfoMessage('Heads up: Critical incidents require a root cause within 24 hours.'); }

1.3 Make a section collapse for cleaner UX

 
function onLoad() { // Section name is the *UI Section* label, not the internal name g_form.setSectionDisplay('Resolution Information', false); }

1.4 Pre-fill from logged-in user

 
function onLoad() { if (!g_form.getValue('caller_id')) { g_form.setValue('caller_id', g_user.userID); } }

1.5 Dynamically switch form view (when a specific condition is met)

 
function onLoad() { if (g_form.getValue('category') == 'network' && g_form.getViewName() !== 'Network') { g_form.setDisplay('please_wait', true); // optional UX element g_form.saveAndReloadWithView('Network'); } }
 

2) onChange: react to field edits

2.1 Priority drives Impact & Urgency

 
function onChange(control, oldValue, newValue) { if (!newValue) return; if (newValue == '1') { // Critical g_form.setValue('impact', '1'); g_form.setValue('urgency', '1'); g_form.showFieldMsg('priority', 'Critical priority sets Impact & Urgency to High.', 'info'); } }

2.2 Validate future date (no past due date)

 
function onChange(control, oldValue, newValue) { if (!newValue) return; var now = new GlideDateTime(); var picked = new GlideDateTime(newValue); if (picked.before(now)) { g_form.showFieldMsg(control, 'Date/time cannot be in the past.', 'error'); g_form.setValue(control, ''); } }

2.3 Enforce attachments for certain categories

 
function onChange(control, oldValue, newValue) { if (g_form.getValue('category') == 'hardware') { var hasAttachments = g_form.hasAttachments(); // client API exists in forms if (!hasAttachments) { g_form.addErrorMessage('Please attach a photo/receipt for hardware requests.'); } } }

2.4 Conditional reference qualifier (filter “Assignment group” by selected CI)

 
function onChange(control, oldValue, newValue) { // Re-qualify assignment_group to teams supporting the chosen CI g_form.setDependentField('assignment_group', 'cmdb_ci'); // Or set a dynamic ref qual: g_form.setQuery('assignment_group', 'support_groupISEMPTY^ORci=' + newValue); }

2.5 Auto-copy caller’s manager (with GlideAjax)

Client Script

 
function onChange(control, oldValue, newValue) { if (!newValue) return; var ga = new GlideAjax('UserUtilsAjax'); ga.addParam('sysparm_name', 'getManagerId'); ga.addParam('sysparm_user', newValue); ga.getXMLAnswer(function(mgrId){ if (mgrId) g_form.setValue('manager', mgrId); }); }

Script Include (client-callable)

 
var UserUtilsAjax = Class.create(); UserUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, { getManagerId: function() { var userId = this.getParameter('sysparm_user'); var u = new GlideRecord('sys_user'); if (u.get(userId) && u.manager) return u.manager.toString(); return ''; } });

Comments

No comments yet.


Log in to post a comment