Category:
Updated on: August 30, 2025  |  0

Client Script Patterns 4

🔧 Client Script Patterns:

Examples & Scenarios

7) Performance & UX helpers

7.1 Batch UI changes to reduce repainting

 
function onLoad() { g_form.flash('short_description', '#fff2a8'); // subtle highlight g_form.setVariablesReadOnly(true); // for catalog g_form.isNewRecord() && g_form.setValue('state', '1'); }

7.2 Debounce expensive onChange logic

 
var debounceTimer; function onChange(control, oldValue, newValue) { clearTimeout(debounceTimer); debounceTimer = setTimeout(function() { // call GlideAjax or heavy logic *after* user stops typing/changing doLookup(newValue); }, 400); }

8) Security, integrity & edge cases

8.1 Prevent user from clearing mandatory due to UI policy flip

 
function onSubmit() { var fields = ['short_description','assignment_group']; for (var i=0;i<fields.length;i++) { if (!g_form.getValue(fields[i])) { g_form.addErrorMessage('Fill all required fields before submitting.'); return false; } } return true; }

8.2 Guard rails when a CI is blacklisted

 
function onChange(control, oldValue, newValue) { if (!newValue) return; var blocked = ['CI1234567890abcdef', 'CIabcdef1234567890']; if (blocked.indexOf(newValue) > -1) { g_form.addErrorMessage('Selected CI is restricted. Choose another.'); g_form.clearValue('cmdb_ci'); } }

9) Troubleshooting checklist

  • Script runs but nothing changes?
    Check exact field name (not label), and that you’re on the correct table/view.

  • Random errors after copy/paste?
    Ensure semi-colons and function signatures match the script type.

  • GlideAjax returns empty?
    Script Include must extend AbstractAjaxProcessor and be Client Callable (in UI).

  • Date comparisons fail?
    Use GlideDateTime for consistent parsing of platform date strings.


10) When to use UI Policies instead

  • Pure visibility/mandatory/read-only rules → UI Policy first (faster, simpler).

  • Client Scripts for complex logic, async lookups, or cross-field calculations.

Comments

No comments yet.


Log in to post a comment