Scripting How to access last worknotes? We can use “current.comments.getJournalEntry(1)” to access last worknotes. hasRole() It returns true if the current user has the specified role or the admin role How to get system property? getProperty(“PROPERTY_NAME “); How to set system property? setProperty(“PROPERTY_NAME “, “property_value”); How to alert Assigned to user name from client script? var p=g_form.getReference(“assigned_to”).name; alert(p); How to access user data from client script g_user.userName g_user.userID g_user.firstName How to access user data from Server Side Script var firstName = gs.getUser().getFirstName(); var email = gs.getUser().getEmail(); getUser(); How to get sys_id from client script alert(g_form.getUniqueValue()); How to get current form table display value in client script alert(g_form.getDisplayValue()); and alert(g_form.getDisplayValue(“assigned_to”)) How to hide the related list from client script hideRelatedList(“REL:SYSIDOFRELATEDLIST”); How to hide section from script setSectionDisplay(“closure_information”, true); How to fire Event from Server-side script eventQueue(“EVENTNAME”, GlideRecord, Parm1, Parm2) How to write GlideRecord on incident to get active records Var gr=New GlideRecord(“incident”); addQuery(“active”,true); query(); if(gr.next()){ info(gr.number); } G-FORM:GlideForm.js is the Javascript class used to customize forms. getValue(‘short_description’) setValue(‘short_description’, “”) addOption(‘priority’, ‘2.5’, ‘2.5 – Moderately High’, 3) getTableName() addErrorMessage(‘This is an error’) addInfoMessage(‘The top five fields in this form are mandatory’) showFieldMsg(‘impact’,’Low impact response time can be one week’,’info’) showFieldMsg(‘impact’,’Low impact not allowed with High priority’,’error’) flash(“incident.number”, “#FFFACD”, 0) g_user: g_user is a global object in GlideUser, can only used in Client Script contains name and role information about the current user. userName userID firstName getClientData(“loginlanguage”) hasRole(‘admin’) hasRoleExactly(‘util’) hasRoleFromList(“itil, maint”) hasRoles() Client Script 1.To alert fullname of user: function onLoad() { var p=g_user.getFullName(); alert(‘This is my first client Script= ‘+p); } 2.To 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 API’s: 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.To make field visible after creating record but not while creating Record function onLoad() { if(g_form.isNewRecord()){ g_form.setVisible(“u_convert_to”,false); } else { g_form.setVisible(“u_convert_to”,true); } } 5.when Assigned To value changes, populte mail id of changed user in email field and populate user name in short description 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.when Assigned To value changes,populate user’s first name in short description function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === ”) { return; } var p = g_form.getValue(“u_assigned_to”); 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 submit form function onSubmit() { //Type appropriate comment here, and begin script below var a = g_form.getValue(“u_first_name”); var b = g_form.getValue(“u_last_name”); var con= confirm( ‘You Entered below datails-n The first name is- ‘+a+’n The last name is- ‘+b+’n Do you confirm the details?’ ); if(con==true){ return; } else{ return 0; } } 8. On Submit function onSubmit() { //Type appropriate comment here, and begin script below var p = g_form.getValue(“u_assigned_to”); var name= g_form.getReference(“u_assigned_to”); g_form.setValue(“u_short_description”, name.first_name); alert(p); } 9. On editing first name in list view (onCellEdit Client Script) function onCellEdit(sysIDs, table, oldValues, newValue, callback) { var saveAndClose = true; alert(sysIDs); alert(table); alert( oldValues); alert(newValue); alert(callback); callback(saveAndClose); } Advance Client Script Client Script: Client Side Objects: 1. G-FORM:GlideForm.js is the Javascript class used to customize forms. g_form.getValue(‘short_description’)g_form.setValue(‘short_description’, “”)g_form.addOption(‘priority’, ‘2.5’, ‘2.5 – Moderately High’, 3)g_form.getTableName()g_form.addErrorMessage(‘This is an error’)g_form.addInfoMessage(‘The top five fields in this form are mandatory’)g_form.showFieldMsg(‘impact’,’Low impact response time can be one week’,’info’)g_form.showFieldMsg(‘impact’,’Low impact not allowed with High priority’,’error’)g_form.flash(“incident.number”, “#FFFACD”, 0) 2. G-User: g_user is a global object in GlideUser. g_user is a Client Script Object and contains name and role information about the current user. g_user.userNameg_user.userIDg_user.firstNameg_user.getClientData(“loginlanguage”)g_user.hasRole(‘admin’)g_user.hasRoleExactly(‘util’)g_user.hasRoleFromList(“itil, maint”)g_user.hasRoles() Client Side Examples: 1. Problem Statement: For New Record show alert. function onLoad() { if (g_form.isNewRecord()){ alert(‘This is new Record’); } else{ alert(‘This is not a new record’); } } ……………………………………………………………………………………………………….. 2. Problem Statement: To make “Convert to” field visible after creating record but not while creating Record function onLoad() { if(g_form.isNewRecord()){ g_form.setVisible(“u_convert_to”,false); } else { g_form.setVisible(“u_convert_to”,true); } } 3. Problem Statement: When Assigned To value changes, populte mail id of changed user in email field and populate user name in short description 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()); } ……………………………………………………………………………………………………….. 4. Problem Statement: Confirm details before submit form function onSubmit() { //Type appropriate comment here, and begin script below var a = g_form.getValue(“u_first_name”); var b = g_form.getValue(“u_last_name”); var con= confirm( ‘You Entered below datails-n The first name is- ‘+a+’n The last name is- ‘+b+’n Do you confirm the details?’ ); if(con==true){ return; } else{ return 0; } } ……………………………………………………………………………………………………….. 5. Problem Statement: Hide the Clouser Information section from form while creating new record function onLoad() {//Type appropriate comment here, and begin script belowif(g_form.isNewRecord()){g_form.setSectionDisplay(“closure_information”, false);}else{g_form.setSectionDisplay(“closure_information”, true)}} ……………………………………………………………………………………………………….. 6. Problem Statement: Remove New state from the choice if it is not New function onLoad() {//Type appropriate comment here, and begin script belowif(g_form.getValue(“state”)==1){}else{g_form.removeOption(“state”,’1′);} } 7. Problem Statement: On change—Comments Make Work Notes Optional function onChange(control, oldValue, newValue, isLoading, isTemplate) {var requireOtherField = (newValue == ”);g_form.setMandatory(‘work_notes’, requireOtherField);} ……………………………………………………………………………………………………….. 8. Problem Statement: On Change- Work Notes Make Comments Optional function onChange(control, oldValue, newValue, isLoading, isTemplate) {var requireOtherField = (newValue == ”) ;g_form.setMandatory(‘comments’, requireOtherField);} 9. Problem Statement: If Caller is a VIP User then priority should be always P1. On Change Client Script on Caller Field function onChange(control, oldValue, newValue, isLoading, isTemplate) {if (isLoading || newValue === ”) {return;}g_form.getReference(‘caller_id’, priorityCallback); } function priorityCallback(caller){ if (caller.vip == ‘true’) {g_form.setValue(“impact”,1);g_form.setValue(“urgency”,1); }} ……………………………………………………………………………………………………….. 10. Problem Statement: Start date should be less than End Date,if not show alert Start date validation –function onChange(control, oldValue, newValue,