Scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-06-2024 07:25 AM
Hi everyone , I am unable to understand this script , can anyone please explain this script how it will work with comments .
function onSubmit() {
var action = g_form.getActionName();
if (action != 'close_incident' && action != 'resolve_incident')
return;
// Close notes and Close code must be on the form
if (!g_form.hasField('close_notes') || !g_form.hasField('close_code'))
return;
var state = '6';
if (action == 'close_incident')
state = '7';
g_form.setValue('incident_state', state);
g_form.setValue('state', state);
g_form.setDisplay('close_notes', true);
g_form.setMandatory('close_notes', true);
g_form.setDisplay('close_code', true);
g_form.setMandatory('close_code', true);
if (g_form.getValue('close_notes') == '' || g_form.getValue('close_code') == '')
return false;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-06-2024 07:33 AM
@Ashok Kumar This is an onSubmit script, written on incident table, gets called when the submit button is pressed.
1. On the 1st line it fetches the action name
2. Line 2 checks if the action name is not close_incident or resolve_incident
3. Line 3 returns
4. Line 4 checks if the form has close_notes and close code field
5. if the above is true returns
6. if the close_incident ui action was clicked then set state, incident_state, make close notes visible close code visible and make close code mandatory
7. if close notes or close code is empty then cancel the form submission.
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-06-2024 07:38 AM
Hi @Ashok kumar
This script is a client-side JavaScript function that runs when a form is submitted in ServiceNow. Here's a breakdown of what it does:
- The function onSubmit() is triggered when a form is submitted.
- It first checks the action name of the form. If the action is not 'close_incident' or 'resolve_incident', the function ends and the form is submitted as usual.
- It then checks if the form has 'close_notes' and 'close_code' fields. If not, the function ends and the form is submitted as usual.
- It sets the 'incident_state' and 'state' fields to '6' or '7' depending on whether the action is 'resolve_incident' or 'close_incident' respectively.
- It then makes the 'close_notes' and 'close_code' fields visible and mandatory on the form.
- Finally, it checks if the 'close_notes' and 'close_code' fields are empty. If either of them is empty, the form submission is stopped and the user is required to fill in these fields.
Here's the bullet point summary:
- The script is a client-side JavaScript function that runs on form submission in ServiceNow.
- It checks the action name of the form.
- If the action is not 'close_incident' or 'resolve_incident', the function ends.
- It checks if the form has 'close_notes' and 'close_code' fields.
- If not, the function ends.
- It sets the 'incident_state' and 'state' fields to '6' or '7' depending on the action.
- It makes the 'close_notes' and 'close_code' fields visible and mandatory.
- It checks if the 'close_notes' and 'close_code' fields are empty.
- If either is empty, the form submission is stopped.
Mark Helpful/Solution 🙂
Regards
Shaqeel
***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.
***********************************************************************************************************************
Regards
Shaqeel