We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Scripted Approvals via Flow Action

tiguin2798
Tera Guru

Hello,

 

I have a need to generate approvals via a script in a custom flow action so I can pass custom inputs into a notification without triggering the OOB "approval.inserted" event. For this I have basically recreated the "Ask for Approvals" flow action step.

 

My script appears to be working as in the attached script it generates system info logs for each user I need an approval for with the correct information, however the approvals are never actually inserted into 'sysapproval_approver'. Is there something I'm  missing in my script here? I am separating out user sys_ids into a string Array and then looping through each to create the approvals. I am doing the same if a group is entered and a system log is created for each expected record.


Per another post the only thing I can see I may be missing to set is the apprRec.source_table, but I do not want to set this directly as I would like this flow action to be reusable for any approvals that need my custom fields.

 

(function execute(inputs, outputs) {

    function normalizeArray(input) {
        if (!input) return [];
		return Array.isArray(input) ? input : [input];
	}

    var approvalUsers = normalizeArray(inputs.approvalUsers);
    var approvalGroups = normalizeArray(inputs.approvalGroups);
	
    var specialInstructions = inputs.specialInstructions || '';
    var approvalReason = inputs.approvalReason || '';
	
	var currentRecordSysId = '';
	if (inputs.currentRecord && inputs.currentRecord.sys_id) {
		currentRecordSysId = inputs.currentRecord.sys_id.toString();
		gs.info('[Approval Script] Valid currentRecord.sys_id found. : + currentRecordSysId');
	}
	if (!currentRecordSysId) {
		gs.error('[Approval Script] No valid currentRecord.sys_id found.');
		outputs.approvals_created = 0;
		return;
	}
    
	var approverArray = [];
	
	for (var i = 0; i < approvalUsers.length; i++) {
		var user = approvalUsers[i];
		var userSysId = (user && typeof user === 'object' && user.getTableName)
			? user.getValue('sys_id')
			: user.toString();
		if (userSysId && approverArray.indexOf(userSysId) === -1) {
			approverArray.push(userSysId);
		}
	}
	
	for (var j = 0; j < approvalGroups.length; j++) {
		var groupSysId = approvalGroups[j];
		var groupGR = new GlideRecord('sys_user_grmember');
		groupGR.addQuery('group', groupSysId);
		groupGR.query();
		
		while (groupGR.next()) {
			var userSysId = groupGR.getValue('user');
			if (approverArray.indexOf(userSysId) === -1) {
				approverArray.push(userSysId);
			}
		}
	}
	
	var approvalsCreated = 0;
    for (var k = 0; k < approverArray.length; k++) {
		var userId = approverArray[k];
		gs.info('[Approval Script] Creating approval for user sys_id: ' + userId);
		
		var apprRec = new GlideRecord('sysapproval_approver');
		apprRec.newRecord();
		apprRec.document_id = currentRecordSysId;
		apprRec.approver = userId;
		apprRec.u_special_instructions = true;
		apprRec.u_special_instructions_for_approval = specialInstructions;
		apprRec.state = 'requested';
		apprRec.sysapproval = currentRecordSysId
		apprRec.approval_reason = approvalReason;
		apprRec.short_description - 'Special Instructions Approval via Flow Action "Approval - Special Instructions"';
		apprRec.insert();
		
		var approvalId = apprRec.insert();
		if (approvalId) {
			approvalsCreated++;
			gs.info('[Approval Script] Approval created: ' + approvalId + ' for user: ' + apprRec.approver + ' document id: ' + currentRecordSysId + ' state: ' + apprRec.state + ' approval reason: ' + approvalReason + ' speical instructions Output: ' + specialInstructions + ' special instructions true/false: ' + apprRec.u_special_instructions);
			gs.eventQueue('special.approval.instructions', apprRec, specialInstructions, null);
		} else {
			gs.error('[Approval Script] Failed to create approval for user: ' + userId)
		}
	}
	
	outputs.approvals_created = approvalsCreated;
	
})(inputs, outputs);

 

1 ACCEPTED SOLUTION

GlideFather
Tera Patron

Hi @tiguin2798,

 

in the Flow Designer there's Flow variable (top-right corner), you can create one and script the logics of who will be the approver(s) and this variable can be then used as a input for the ootb action Ask for Approval as a data pill.

 

If it is more complex, maybe you can consider Decision tables for that as well.

 


✂-----Cutting-out-the---✦AI-noise✦---All-replies-written-and-vouched-for-by-GlideFather---

View solution in original post

3 REPLIES 3

GlideFather
Tera Patron

Hi @tiguin2798,

 

in the Flow Designer there's Flow variable (top-right corner), you can create one and script the logics of who will be the approver(s) and this variable can be then used as a input for the ootb action Ask for Approval as a data pill.

 

If it is more complex, maybe you can consider Decision tables for that as well.

 


✂-----Cutting-out-the---✦AI-noise✦---All-replies-written-and-vouched-for-by-GlideFather---

Thank you, it turned out the apprRec.source_table is what I was missing. I was able to create an input variable for the source table and set it this way in the script from input.

Perfect that you found the way to fix it, @tiguin2798!


✂-----Cutting-out-the---✦AI-noise✦---All-replies-written-and-vouched-for-by-GlideFather---