← Script library

UI Actions

Send for Approval Button

Add a form button to trigger approval workflow with validation.

JavaScript
// Server-side code
(function() {
  // Validation: Check required fields
  var errors = [];

  if (current.isNil('short_description')) {
    errors.push('Short Description is required');
  }

  if (current.isNil('description')) {
    errors.push('Description is required');
  }

  if (current.isNil('risk')) {
    errors.push('Risk assessment is required');
  }

  if (current.isNil('impact')) {
    errors.push('Impact is required');
  }

  if (current.isNil('start_date') || current.isNil('end_date')) {
    errors.push('Implementation dates are required');
  }

  // Show errors if validation fails
  if (errors.length > 0) {
    gs.addErrorMessage('Cannot send for approval. Please fix the following:\n' + errors.join('\n'));
    return;
  }

  // Check if already approved or in approval
  if (current.approval.toString() !== 'not requested') {
    gs.addErrorMessage('This change is already in approval process (Status: ' + current.approval.getDisplayValue() + ')');
    return;
  }

  // Set approval to requested
  current.approval = 'requested';
  current.work_notes = 'Approval requested by ' + gs.getUserName();
  current.update();

  // Trigger approval workflow
  var workflow = new Workflow();
  var workflowId = workflow.startFlow(
    'change_request_approval',  // Workflow name/sys_id
    current,
    'request',  // Operation
    {           // Variables
      requested_by: gs.getUserID(),
      request_date: new GlideDateTime()
    }
  );

  if (workflowId) {
    gs.addInfoMessage('Change request sent for approval. Workflow ID: ' + workflowId);

    // Send notification to approvers
    gs.eventQueue('change.approval.requested', current, gs.getUserID(), current.sys_id);
  } else {
    gs.addErrorMessage('Failed to start approval workflow');
  }
})();

How to use it

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

Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.