Real time ServicceNow Developer ClientScript interview Questions And Answers

Questions

  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?
  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?
  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?
  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.
  5. Develop a Client Script to dynamically show/hide the “Category” field based on the value selected in the “Incident Type” field.
  6. Write a Client Script to pre-populate a user’s department based on their manager selection in a form.
  7. Create a Client Script that disables a “Submit” button if the value in a text field is belowza certain character count.
  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.

 

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:
    • Use an onChange Client Script.
    • Add a condition to check the value of the multiple-choice field.
    • Use the g_form.setDisplay() method to hide or show the specific field based on the selected value.
  • Example Code:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue === ”) {

        return;

    }

    if (newValue === ‘specific_value’) {

        g_form.setDisplay(‘target_field_name’, false); // Hide field

    } else {

        g_form.setDisplay(‘target_field_name’, true); // Show field

    }

}

  1. 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:
    • Use an onSubmit Client Script.
    • Check if the required field is empty using g_form.getValue().
    • Use g_form.addErrorMessage() to alert the user and return false to prevent submission.
  • Example Code:

function onSubmit() {

    var requiredField = g_form.getValue(‘required_field_name’);

    if (!requiredField) {

        g_form.addErrorMessage(‘Please fill out the required field.’);

        return false; // Prevent form submission

    }

    return true; // Allow form submission

}

  1. 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:
    • Use an onSubmit Client Script.
    • Fetch the values of both fields and validate the condition.
    • Prevent submission if the condition fails.
  • Example Code:

function onSubmit() {

    var field1 = g_form.getValue(‘field1_name’);

    var field2 = g_form.getValue(‘field2_name’);

    if (field1 === ‘specific_value’ && field2 === ”) {

        g_form.addErrorMessage(‘Field2 must not be empty if Field1 is “specific_value”.’);

        return false; // Prevent submission

    }

    return true;

}

  1. 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:
    • Use an onChange Client Script.
    • Check the value of the priority field and use g_form.setMandatory() to toggle the field’s mandatory state.
  • Example Code:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue === ”) {

        return;

    }

    if (newValue === ‘High’) {

        g_form.setMandatory(‘target_field_name’, true); // Make field mandatory

    } else if (newValue === ‘Low’) {

        g_form.setMandatory(‘target_field_name’, false); // Make field optional

    }

}

  1. Develop a Client Script to dynamically show/hide the “Category” field based on the value selected in the “Incident Type” field.
  • Implementation:
    • Use an onChange Client Script to monitor changes in the “Incident Type” field.
    • Use g_form.setDisplay() to show or hide the “Category” field.
  • Example Code:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue === ”) {

        return;

    }

    if (newValue === ‘specific_incident_type’) {

        g_form.setDisplay(‘category’, true); // Show Category field

    } else {

        g_form.setDisplay(‘category’, false); // Hide Category field

    }

}

  1. Write a Client Script to pre-populate a user’s department based on their manager selection in a form.
  • Implementation:
    • Use an onChange Client Script.
    • Fetch the selected manager’s department using a GlideAjax or GlideRecord query.
    • Set the value of the department field using g_form.setValue().
  • Example Code:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue === ”) {

        return;

    }

    var ga = new GlideAjax(‘GetManagerDepartment’);

    ga.addParam(‘sysparm_name’, ‘getDepartment’);

    ga.addParam(‘sysparm_manager_id’, newValue);

    ga.getXMLAnswer(function(response) {

        var department = response;

        g_form.setValue(‘department_field_name’, department); // Pre-populate department

    });

}

  1. Create a Client Script that disables a “Submit” button if the value in a text field is below a certain character count.
  • Implementation:
    • Use an onChange Client Script.
    • Check the character count of the text field value using length property.
    • Use g_form.setDisabled() to disable or enable the “Submit” button.
  • Example Code:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading) {

        return;

    }

    if (newValue.length < 10) { // Minimum character count

        g_form.setDisabled(‘submit_button_field_name’, true); // Disable Submit button

    } else {

        g_form.setDisabled(‘submit_button_field_name’, false); // Enable Submit button

    }

}

  1. 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:
    • Use an onSubmit Client Script.
    • Check the “Request Type” field value and validate the “Requested For” field.
    • Prevent form submission and display an error message if validation fails.
  • Example Code:

function onSubmit() {

    var requestType = g_form.getValue(‘request_type’);

    var requestedFor = g_form.getValue(‘requested_for’);

    if (requestType === ‘Service’ && requestedFor === ”) {

        g_form.addErrorMessage(‘”Requested For” cannot be empty when “Request Type” is set to “Service”.’);

        return false; // Prevent form submission

    }

    return true;

}