UI Actions
Manually Trigger Approval Process
Trigger a custom approval workflow with validation and user selection of approvers.
// Server-side code
(function() {
// Validation
if (current.isNewRecord()) {
gs.addErrorMessage('Please save the record before requesting approval');
return;
}
// Check if already in approval
if (current.approval.toString() !== 'not requested') {
gs.addErrorMessage('This change is already in approval process');
return;
}
// Validate required fields
var requiredFields = [
{field: 'short_description', label: 'Short Description'},
{field: 'risk', label: 'Risk'},
{field: 'impact', label: 'Impact'},
{field: 'implementation_plan', label: 'Implementation Plan'}
];
var missingFields = [];
for (var i = 0; i < requiredFields.length; i++) {
if (current.isNil(requiredFields[i].field)) {
missingFields.push(requiredFields[i].label);
}
}
if (missingFields.length > 0) {
gs.addErrorMessage('Cannot request approval. Missing required fields: ' + missingFields.join(', '));
return;
}
try {
// Determine approval level based on risk and impact
var risk = current.risk.toString();
var impact = current.impact.toString();
var approvalLevel = 'standard';
if (risk === '1' || impact === '1') { // High risk or high impact
approvalLevel = 'cab'; // Change Advisory Board
} else if (risk === '2' || impact === '2') {
approvalLevel = 'manager'; // Manager approval
} else {
approvalLevel = 'standard'; // Standard approval
}
// Get approvers based on level
var approvers = [];
if (approvalLevel === 'cab') {
// Get CAB members
var grCAB = new GlideRecord('sys_user_grmember');
grCAB.addQuery('group.name', 'Change Advisory Board');
grCAB.query();
while (grCAB.next()) {
approvers.push(grCAB.user.toString());
}
} else if (approvalLevel === 'manager') {
// Get manager of assigned group
if (current.assignment_group) {
var grGroup = new GlideRecord('sys_user_group');
if (grGroup.get(current.assignment_group) && grGroup.manager) {
approvers.push(grGroup.manager.toString());
}
}
// Add change manager
var grRole = new GlideRecord('sys_user_has_role');
grRole.addQuery('role.name', 'change_manager');
grRole.addQuery('user.active', 'true');
grRole.query();
while (grRole.next()) {
approvers.push(grRole.user.toString());
}
} else {
// Standard approval - assignment group manager
if (current.assignment_group) {
var grGroup = new GlideRecord('sys_user_group');
if (grGroup.get(current.assignment_group) && grGroup.manager) {
approvers.push(grGroup.manager.toString());
}
}
}
// Remove duplicates
approvers = approvers.filter(function(item, pos) {
return approvers.indexOf(item) === pos;
});
if (approvers.length === 0) {
gs.addErrorMessage('No approvers found for this change. Please contact Change Management.');
return;
}
// Create approval records
var approvalCount = 0;
for (var i = 0; i < approvers.length; i++) {
var grApproval = new GlideRecord('sysapproval_approver');
grApproval.initialize();
grApproval.source_table = 'change_request';
grApproval.sysapproval = current.sys_id;
grApproval.approver = approvers[i];
grApproval.state = 'requested';
grApproval.due_date = new GlideDateTime();
grApproval.due_date.addDaysLocalTime(3); // 3 days to approve
grApproval.insert();
approvalCount++;
}
// Update change request
current.approval = 'requested';
current.work_notes = 'Approval requested by ' + gs.getUserName() + '. ' +
'Level: ' + approvalLevel + ', ' +
'Approvers: ' + approvalCount;
current.update();
// Send notifications
for (var i = 0; i < approvers.length; i++) {
gs.eventQueue('change.approval.requested', current, approvers[i], current.sys_id);
}
// Show success message
gs.addInfoMessage('Approval requested from ' + approvalCount + ' approver(s). Level: ' + approvalLevel);
} catch (e) {
gs.addErrorMessage('Error requesting approval: ' + e.message);
gs.error('Approval trigger error for ' + current.number + ': ' + e.message);
}
})();How to use it
1. Create a new UI Action on change_request table 2. Set Name to 'Request Approval' 3. Check 'Form button' checkbox 4. Uncheck 'Client' checkbox 5. Set Order: 100 6. Add condition: current.approval=='not requested' && !current.isNewRecord() 7. Paste the code above 8. Customize approval levels and approver logic 9. Update notification events 10. Test with various risk/impact combinations
Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.