Client Script In Service-now
<h2>Client Scripts in ServiceNow – Fundamentals Explained</h2> <h3>1. Alert Full Name of Logged-in User</h3><pre><code>function onLoad() { var p = g_user.getFullName(); alert(‘This is my first client Script = ‘ + p);}</code></pre> <h3>2. Alert If the Record Is New or Not</h3><pre><code>function onLoad() { if (g_form.isNewRecord()) { alert(‘This is new Record’); } else { alert(‘This is not a new record’); }}</code></pre> <h3>3. Use of Different APIs</h3><pre><code>function onLoad() { g_form.setReadOnly(‘u_email’, true); g_form.setValue(‘u_description’, ‘hiiiiiii’); g_form.clearOptions(‘u_category’); g_form.getLabelOf(‘u_number’); g_form.addInfoMessage(‘hi’); g_form.setMandatory(‘u_first_name’, true); g_form.setMandatory(‘u_last_name’, true); if (g_user.hasRole(‘admin’)) { g_form.removeOption(‘u_state’, ‘Maharashtra’); }}</code></pre> <h3>4. Show Field After Record Creation (Hide in New Record)</h3><pre><code>function onLoad() { if (g_form.isNewRecord()) { g_form.setVisible(“u_convert_to”, false); } else { g_form.setVisible(“u_convert_to”, true); }}</code></pre> <h3>5. Populate Email & Short Description When Assigned To Changes</h3><pre><code>function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === ”) { return; } var p = g_form.getReference(“u_assigned_to”).email; g_form.setValue(“u_email”, p); g_form.setMandatory(“u_first_name”, true); g_form.setValue(“u_short_description”, ‘This is :’ + g_user.getFullName());}</code></pre> <h3>6. Populate User’s First Name in Short Description</h3><pre><code>function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === ”) { return; } var name = g_form.getReference(“u_assigned_to”); g_form.setValue(“u_short_description”, ‘This is ‘ + name.first_name); alert(newValue); alert(oldValue);}</code></pre> <h3>7. Confirm Details Before Form Submit</h3><pre><code>function onSubmit() { var a = g_form.getValue(“u_first_name”); var b = g_form.getValue(“u_last_name”); var con = confirm( ‘You Entered below details -\n’ + ‘The first name is: ‘ + a + ‘\n’ + ‘The last name is: ‘ + b + ‘\n’ + ‘Do you confirm the details?’ ); if (con == true) { return; } else { return false; }}</code></pre> Client Scripts in ServiceNow – Fundamentals Explained 1. Alert Full Name of Logged-in User function onLoad() { var p = g_user.getFullName(); alert(‘This is my first client Script = ‘ + p); } 2. Alert If the Record Is New or Not function onLoad() { if (g_form.isNewRecord()) { alert(‘This is new Record’); } else { alert(‘This is not a new record’); } } 3. Use of Different APIs function onLoad() { g_form.setReadOnly(‘u_email’, true); g_form.setValue(‘u_description’, ‘hiiiiiii’); g_form.clearOptions(‘u_category’); g_form.getLabelOf(‘u_number’); g_form.addInfoMessage(‘hi’); g_form.setMandatory(‘u_first_name’, true); g_form.setMandatory(‘u_last_name’, true); if (g_user.hasRole(‘admin’)) { g_form.removeOption(‘u_state’, ‘Maharashtra’); } } 4. Show Field After Record Creation (Hide in New Record) function onLoad() { if (g_form.isNewRecord()) { g_form.setVisible(“u_convert_to”, false); } else { g_form.setVisible(“u_convert_to”, true); } } 5. Populate Email & Short Description When Assigned To Changes function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === ”) { return; } var p = g_form.getReference(“u_assigned_to”).email; g_form.setValue(“u_email”, p); g_form.setMandatory(“u_first_name”, true); g_form.setValue(“u_short_description”, ‘This is :’ + g_user.getFullName()); } 6. Populate User’s First Name in Short Description function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === ”) { return; } var name = g_form.getReference(“u_assigned_to”); g_form.setValue(“u_short_description”, ‘This is ‘ + name.first_name); alert(newValue); alert(oldValue); } 7. Confirm Details Before Form Submit function onSubmit() { var a = g_form.getValue(“u_first_name”); var b = g_form.getValue(“u_last_name”); var con = confirm( ‘You Entered below details -n’ + ‘The first name is: ‘ + a + ‘n’ + ‘The last name is: ‘ + b + ‘n’ + ‘Do you confirm the details?’ ); if (con == true) { return; } else { return false; } }
