Basic Scripting Questions and Answers.
Hi Guys,
Below are some Basic scripting questions and their answers that every beginner must go through. Hit helpful if you get benefited from it and save it in the bookmark for further usage.
Edit 1:- Check out my new Article for real-life use cases:-
Question 1:- Create an alert message using client script:-
Answer:- Navigate to System Definition- Client Script - New
Question 2 :- Create a business rule to print Hello:-
Answer:-
Write it before the update.
Question 3:- Remove UI policy for Problem field and add same validation using client script.
Answer:-
Navigate to Client Script-New
Write Script as:-
function onChange(control, oldValue, newValue, isLoading) {if (isLoading) {return;}if(newValue.toString() == 'true'){g_form.setReadOnly('problem_id', true);}else{g_form.setReadOnly('problem_id', false);}
}
Question 4:- When the category is Software, the assignment group will be Software. Use business rule.
Answer:-
create before insert business rule on the incident table.
Condition: category == 'software'
Script:
(function executeRule(current, previous /null when async/) {// Add your code herecurrent.assignment_group.setDisplayValue('Software');
})(current, previous);
Question 5:- Create a common script for emails that will contain INC number, short description, Priority and configuration item:-
Answer:-
Create Email Script.
Create Notification.
Add mail script template in notification body by using:-
${mail_script:script name}
Script:-
Question 6:-Auto-Populate user's email and user id when the user changes.
Answer:-
Client Script:-
function onChange(control, oldValue, newValue, isLoading) {if (isLoading) {return;}if(newValue == ''){g_form.clearValue('u_vip_email');g_form.clearValue('u_user_id');}var ga = new GlideAjax('checkRecords');ga.addParam('sysparm_name', "getUserDetails");ga.addParam('sysparm_userID', g_form.getValue('u_reference_1'));ga.getXMLAnswer(function(answer){var parser = JSON.parse(answer);alert(answer);g_form.setValue('u_vip_email', parser.email);g_form.setValue('u_user_id', parser.user_name);});//Type appropriate comment here and begin script below
}
Script Include:-
var checkRecords = Class.create();checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {getUserDetails: function(){var id = this.getParameter('sysparm_userID');var obj = {};var gr = new GlideRecord('sys_user');gr.addQuery('sys_id', id);gr.query();if(gr.next()){obj["user_name"] = gr.user_name.toString();obj["email"] = gr.email.toString();}return JSON.stringify(obj);},type: 'checkRecords'
});
Question 7:-Create a new field Date and add validation for the date field so that it takes only future date.
Answer:-
Config-Form Design-Add date field
Navigate to UI policy:-
Script Section:-
Execute If true:-
function onCondition(){ g_form.setValue('date',' '); alert('Enter Valid Date, It cannot be past date');
}
Question 8:- Problem should get created when a 'is problem required?' checkbox is checked and when incident gets resolved and problem id will be stored in problem field. The incident should not get resolved until all incident tasks associated with it gets closed.
Answer:-
Navigate to Business Rules
Script:-
(function executeRule(current, previous /null when async/) {//check if there are any incident tasks openvar task = new GlideRecord("incident_task");task.addEncodedQuery("stateIN-5,1,2");task.query();if(task.next()) {gs.addErrorMessage("You cannot close incident if incident tasks are still open.");current.setAbortAction(true);}else{var gr = new GlideRecord("problem");gr.initialize();gr.short_description=current.short_description;var prob_id = gr.insert();current.problem_id=prob_id;}
})(current, previous);
Question 9:-Problem related to the incident should get displayed in a related list on the incident.
Answer:-
Need to create defined relationshipsNavigate to RelationshipApplies to Table - incidentQueries from the table - problem
Script:-
https://www.servicenow.com/community/developer-articles/basic-scripting-questions-and-answers/ta-p/2296560