Skip to Content

Send for Approval Button

Add a form button to trigger approval workflow with validation.

Form Button Table: change_request
#ui-action #form-button #approval #workflow #validation #server-side

Script Code

JavaScript
1// Server-side code
2(function() {
3  // Validation: Check required fields
4  var errors = [];
5
6  if (current.isNil('short_description')) {
7    errors.push('Short Description is required');
8  }
9
10  if (current.isNil('description')) {
11    errors.push('Description is required');
12  }
13
14  if (current.isNil('risk')) {
15    errors.push('Risk assessment is required');
16  }
17
18  if (current.isNil('impact')) {
19    errors.push('Impact is required');
20  }
21
22  if (current.isNil('start_date') || current.isNil('end_date')) {
23    errors.push('Implementation dates are required');
24  }
25
26  // Show errors if validation fails
27  if (errors.length > 0) {
28    gs.addErrorMessage('Cannot send for approval. Please fix the following:\n' + errors.join('\n'));
29    return;
30  }
31
32  // Check if already approved or in approval
33  if (current.approval.toString() !== 'not requested') {
34    gs.addErrorMessage('This change is already in approval process (Status: ' + current.approval.getDisplayValue() + ')');
35    return;
36  }
37
38  // Set approval to requested
39  current.approval = 'requested';
40  current.work_notes = 'Approval requested by ' + gs.getUserName();
41  current.update();
42
43  // Trigger approval workflow
44  var workflow = new Workflow();
45  var workflowId = workflow.startFlow(
46    'change_request_approval',  // Workflow name/sys_id
47    current,
48    'request',  // Operation
49    {           // Variables
50      requested_by: gs.getUserID(),
51      request_date: new GlideDateTime()
52    }
53  );
54
55  if (workflowId) {
56    gs.addInfoMessage('Change request sent for approval. Workflow ID: ' + workflowId);
57
58    // Send notification to approvers
59    gs.eventQueue('change.approval.requested', current, gs.getUserID(), current.sys_id);
60  } else {
61    gs.addErrorMessage('Failed to start approval workflow');
62  }
63})();

How to Use

1. Create a new UI Action on change_request table 2. Set Name to "Send for Approval" 3. Check "Form button" checkbox 4. Uncheck "Client" checkbox 5. Set Order: 50 6. Add condition: current.approval=='not requested' && !current.isNewRecord() 7. Paste the code above 8. Customize required fields validation 9. Update workflow name to match your approval workflow 10. Test by creating a change request and clicking the button

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts