Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Workflow Approval Condition Not Working

MuhammadAqS
Tera Contributor

Hi,

I have a workflow for a record producer, that have multiple approvers (like HR Owner, Finance Manager, Business Manager approvals). And I had to add a new approval at the end that goes to 2 individuals Simon and Ed. I have added this new approval but I want to check that whether Simon or Ed has already approved it or not, as a HR Owner, Finance Manager or Business Manager. If they already approved it, so I don't need to send this new approval to them.

I have added an If activity before my new approval for Simon and Ed. Below is the code for that If condition, but it is not working:

function ifScript() {
var simonGriffinUsers = ['ed.griffin', 'simon.belfer']; 
var approvalFound = false;

// Query sysapproval_approver to check if either Ed or Simon has already approved the request
var approverGr = new GlideRecord('sysapproval_approver');
approverGr.addQuery('approver', 'IN', simonGriffinUsers); // Filter for Ed Griffin or Simon Belfer
approverGr.addQuery('sysapproval', current.sysapproval); // Ensure it's for the same request
approverGr.addQuery('state', 'approved'); // Look only for approved records
approverGr.query();

while (approverGr.next()) {
// Log or check the approver roles
var approver = approverGr.approver.name;
gs.log("Approver found: " + approver);

// Check if Ed or Simon has approved already as HR Owner or Finance Approver
if (approver == 'ed.griffin' || approver == 'simon.belfer') {
approvalFound = true;
break;
}
}

// Return 'yes' to skip if already approved, 'no' otherwise
if (approvalFound) {
return 'yes'; // Skip sending another approval
}

return 'no'; // Send approval as they haven't approved yet
}



4 REPLIES 4

Eshwar Reddy
Kilo Sage

Hi @MuhammadAqS 
Try be/ow script
var simonGriffinUsers = ['ed.griffin', 'simon.belfer'];
var approvalFound = false;


var approverGr = new GlideRecord('sysapproval_approver');
approverGr.addQuery('approver', 'IN', simonGriffinUsers.join(',')); 
approverGr.addQuery('sysapproval', current.sys_id); 
approverGr.addQuery('state', 'approved');
approverGr.query();


while (approverGr.next()) {
var approver = approverGr.approver.name; 
gs.info("Approver found: " + approver); 


if (simonGriffinUsers.indexOf(approver) !== -1) {
approvalFound = true; // Set to true if either has approved
break; 
}
}


return approvalFound ? 'yes' : 'no';
}

Hi @Eshwar Reddy,

Thank you so much for your response, but it still sending the approval notification to Ed, when he has already approved that.

Toderean alexan
Tera Contributor

Hello @MuhammadAqS 

  Can we see the workflow? I think you put the code in an if node, it should work, try a debug with the help of messages.

Hi @Toderean alexan,

Sure, it is a very complex workflow, so I have attached only that If part and approval part.