Auto approve when the requestor is same as approver?

Rajavardhini
Tera Contributor

Hi All,

I have user below code in business rule on sn_hr_core_case_talent_management table when HRC should auto approved when opened by is one of the approval group

(function executeRule(current, previous /*null when async*/) {
var user = current.opened_by;

    var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval', sysid);
gr.addQuery('approver', user);
gr.addQuery('state', 'requested');
gr.query();

if (gr.next()) {


    gr.state = 'approved';
    gr.comments = 'auto approved as approver and requestor is same';
    gr.setWorkflow(false);
    gr.update();


}
})(current, previous);

It is not working for me. and to write in workflow based on conditions approval group is called like below

The Code is in Approval User activity.

var answer = [];
var grRitm = new GlideRecord('sn_hr_core_case_talent_management');
grRitm.get(current.sys_id);
var mrvs = grRitm.variables.training_details;
var rowCount = mrvs.getRowCount();
var idList = [];

for (var i = 0; i < rowCount; i++) {
    var row = mrvs.getRow(i);

    if (row.countries_applicable.contains(',')) {
      
        var ids = row.countries_applicable.split(',');
        for(var j = 0; j < ids.length; j++) {
            idList.push(ids[j]);
        }
    } else {
        
        idList.push(row.countries_applicable);
    }

 

}
var count = 0;
var case1 = false;
var case2 = false;
while (count < idList.length) {

    if (idList[count] == '37428923db211300699afd9eaf9619d4' || idList[count] == 'dfd024bfdb361b00699afd9eaf9619f1') {
         case1 = true;
         
    } else {
         case2  = true;
      
    }
    count++;
}

// both spain & portugal only
if (case1 && !case2) {
 //answer.push('ee8e461adb0a67405004608505961928');
    answer.push("a6ef461edb0a6740500460850596194a"); //RITM0575629 = ozambrano@ccep.com
// both spain & portugal and other country
} else if (case1 && case2) {
// no spain & portugal
   answer.push('1b4654651b0d701010c27c95464bcbc0'); //This is the approval group,if the requestor is one of this group the approval should be auto approved.

} else {

    answer.push('1b4654651b0d701010c27c95464bcbc0');//This is the approval group,if the requestor is one of this group the approval should be auto approved.
}

Can anybody help me on this requirement how to proceed it.

2 REPLIES 2

Valmik Patil1
Kilo Sage

Hi Rajavardhini,

Please try updating your BR as below,

(function executeRule(current, previous /*null when async*/) {
var user = current.opened_by.toString();

    var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id',current.sys_id); // refers to current record in approver table
gr.addQuery('approver', user);
gr.addQuery('state', 'requested');
gr.query();

if (gr.next()) {


    gr.state = 'approved';
    gr.comments = 'auto approved as approver and requestor is same';
    gr.setWorkflow(false);
    gr.update();


}
})(current, previous);

Also Please validate background names of field and try adding logs(to debug if any issues) to check if values are correct or not.

Also check for BR condition once

Please let me know if you need any other help

Thanks,

Valmik

Mark Manders
Mega Patron

Are you sure about this requirement? Something needs to be approved for a reason. And if you request something, someone else needs to say 'OK, that's fine'. I do understand that eventually you reach the highest level within an organization so there is no 'boss' anymore to approve, but even then: should the boss just get anything he requests?

And in this case: you have a group that is approving. So why not turn it around and make sure the approval isn't issued to the requestor, but only to the others in the group. That way control is still there (which could also be an audit thingy, I noticed with previous customers I worked with).

If my answer helped you in any way, please then mark it as helpful.

Mark


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark