Client script
Client script
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:
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:
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.
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.