0% found this document useful (0 votes)
7 views

Client script

The document provides a series of real-world interview questions and answers related to Client Scripts in ServiceNow. Each scenario outlines specific implementations using onChange or onSubmit Client Scripts to manage field visibility, mandatory fields, and form validation based on user input. Example code snippets are included for each scenario to illustrate the implementation process.

Uploaded by

prasadhatte51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Client script

The document provides a series of real-world interview questions and answers related to Client Scripts in ServiceNow. Each scenario outlines specific implementations using onChange or onSubmit Client Scripts to manage field visibility, mandatory fields, and form validation based on user input. Example code snippets are included for each scenario to illustrate the implementation process.

Uploaded by

prasadhatte51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

SERVICENOW REAL WORLD SCENARIO

BASED INTERVIEW QUESTIONS & ANSWERS


1. A client requests that a specific field is hidden when a
particular value is selected in a multiple-choice field.
How would you implement this using a Client Script?
• Implementation:
o Use an onChange Client Script.

o Add a condition to check the value of the multiple-


choice field.

o Use the g_form.setDisplay() method to hide or show


the specific field based on the selected value.
• Example Code:

1. function onChange(control, oldValue,


newValue, isLoading) {
2. if (isLoading || newValue === ”) {
3. return;
4. }
5. if (newValue === ‘specific_value’) {
6. g_form.setDisplay(‘target_field_name’,
false); // Hide field
7. } else {
8. g_form.setDisplay(‘target_field_name’,
true); // Show field
9. }
10. }
11.
2. A user fills out a form and submits it, but you want to
prevent the submission if one of the required fields is left
blank. How would you handle this with a Client Script?
• Implementation:
o Use an onSubmit Client Script.

o Check if the required field is empty


using g_form.getValue().

o Use g_form.addErrorMessage() to alert the user


and return false to prevent submission.
• Example Code:
1. function onSubmit() {
2. var requiredField =
g_form.getValue(‘required_field_name’);
3. if (!requiredField) {
4. g_form.addErrorMessage(‘Please fill out
the required field.’);
5. return false; // Prevent form
submission
6. }
7. return true; // Allow form submission
8. }
9.
3. You are required to dynamically validate a field based on the
value of another field. How would you implement this using
a Client Script before the form is submitted?
• Implementation:
o Use an onSubmit Client Script.

o Fetch the values of both fields and validate


the condition.
o Prevent submission if the condition fails.
• Example Code:

1. function onSubmit() {
2. var field1 =
g_form.getValue(‘field1_name’);
3. var field2 =
g_form.getValue(‘field2_name’);
4. if (field1 === ‘specific_value’ &&
field2
=== ”) {
5. g_form.addErrorMessage(‘Field2 must not be
empty if Field1 is “specific_value”.’);
6. return false; // Prevent submission
7. }
8. return true;
9. }
10.
4. Create a Client Script that makes a field mandatory when
the user selects “High” from a priority field and optional
when “Low” is selected.
• Implementation:
o Use an onChange Client Script.
o Check the value of the priority field and use
g_form.setMandatory() to toggle the field’s
mandatory state.
• Example Code:

1. function onChange(control, oldValue,


newValue, isLoading) {
2. if (isLoading || newValue === ”) {
3. return;
4. }
5. if (newValue === ‘High’) {

6. g_form.setMandatory(‘target_field_name’,
true); // Make field mandatory
7. } else if (newValue === ‘Low’) {

8. g_form.setMandatory(‘target_field_name’,
false); // Make field optional
9. }
10. }
11.
5. Develop a Client Script to dynamically show/hide the
“Category” field based on the value selected in the
“Incident Type” field.
• Implementation:
o Use an onChange Client Script to monitor changes in
the “Incident Type” field.
o Use g_form.setDisplay() to show or hide the
“Category” field.
• Example Code:

1. function onChange(control, oldValue,


newValue, isLoading) {
2. if (isLoading || newValue === ”) {
3. return;
4. }
5. if (newValue ===
‘specific_incident_type’)
{
6. g_form.setDisplay(‘category’, true); //
Show Category field
7. } else {
8. g_form.setDisplay(‘category’,
false);
/ Hide Category field
9. }
10. }
11.
6. Write a Client Script to pre-populate a user’s
department based on their manager selection in a form.
• Implementation:
o Use an onChange Client Script.

o Fetch the selected manager’s department using


a GlideAjax or GlideRecord query.

o Set the value of the department field


using g_form.setValue().
• Example Code:

1. function onChange(control, oldValue,


newValue, isLoading) {
2. if (isLoading || newValue === ”) {
3. return;
4. }
5. var ga = new

6. ga.addParam(‘sysparm_name’,
‘getDepartment’);
7. ga.addParam(‘sysparm_manager_id’,
newValue);
8. ga.getXMLAnswer(function(response) {
9. var department = response;
10.
g_form.setValue(‘department_field_name’
, department); // Pre-populate department
11. });
12. }
13.
7. Create a Client Script that disables a “Submit” button if
the value in a text field is below a certain character count.
• Implementation:
o Use an onChange Client Script.

o Check the character count of the text field value


using length property.
o Use g_form.setDisabled() to disable or enable
the “Submit” button.
• Example Code:

1. function onChange(control, oldValue,


newValue, isLoading) {
2. if (isLoading) {
3. return;
4. }
5. if (newValue.length < 10) { // Minimum
character count

6. g_form.setDisabled(‘submit_button_field_
name’, true); // Disable Submit button
7. } else {

8. g_form.setDisabled(‘submit_button_field_
name’, false); // Enable Submit button
9. }
10. }
11.
8. Develop a Client Script to ensure that if the “Request Type”
field is set to “Service,” the “Requested For” field must not
be empty.
• Implementation:
o Use an onSubmit Client Script.

o Check the “Request Type” field value and validate


the “Requested For” field.

o Prevent form submission and display an error message


if validation fails.
• Example Code:
1. function onSubmit() {
2. var requestType =
g_form.getValue(‘request_type’);
3. var requestedFor =
g_form.getValue(‘requested_for’);
4. if (requestType === ‘Service’ &&
requestedFor === ”) {
5. g_form.addErrorMessage(‘”Requested For”
cannot be empty when “Request Type” is set to
“Service”.’);
6. return false; // Prevent form
submission
7. }
8. return true;
9. }
10.

You might also like