Trigger new approval if current approver is changed in cost center

raj149
Giga Guru

Hello Experts,

 

After rising a request it will wait for cost center approval (cost center approver is present in cmn_cost_center table).

now my requirement is 

current request is waiting for cost center approval but approver is changed in cost center table. now i want to trigger a new approval for current request.

 

means previous.approver !=current.approver  then we need to trigger a new approval based on selected cost center name in sc_request table.

 

How to achieve this by using schedule job...?

 

Best Regards,

Raj

5 REPLIES 5

Mark Manders
Mega Patron

Why do you want this to be done with a scheduled job? Use a flow. It's way easier.

On change of the approver on the cost center, you lookup all approval records related to that cost center and create new ones for the new approver and set the old ones to no longer required or canceled (be sure to test this thoroughly, you don't want the cancelation of the old ones to trigger closing the requests).


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

Hello @Mark Manders 

My customer ask is we need to trigger new approval on non business hours ..that means defiantly i need to use schedule job. 

Harish KM
Kilo Patron
Kilo Patron

Hi @raj149 here i tested using after update BR on cost center table, so here if manager changes on the cost  center table , the below BR will trigger. This BR glides ritm table and checks catalog name to get ritm sysid and then glides approval table to cancel the current approver and inserts a new approval to the ritm.

 

you can modify accordingly

// trigger BR when manager changes

var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('cat_item.name', 'Standard Laptop'); // only to run on standard laptop approvers
    ritm.query();
    if (ritm.next()) {
        gs.info(ritm.sys_id);
        var app = new GlideRecord('sysapproval_approver');
        app.addQuery('sysapproval', ritm.sys_id);
        app.query();
        while (app.next()) {
            gs.info(app.approver.getDisplayValue());
            app.state = 'cancelled';
            app.update();

        }
        var appr = new GlideRecord('sysapproval_approver');
        appr.initialize();
        appr.sysapproval = ritm.sys_id;
        appr.approver =current.manager;
        appr.state = 'requested';
        appr.insert();
    }
Regards
Harish

Hello @Harish KM 

Tested with below after update business rule on level 1 approver changes in cost center table but it is not working .

 

var req = new GlideRecord('sc_request');
    req.addEncodedQuery('request_state=requested');
    req.query();
req.setLimit(1);
    if (req.next()) {
        gs.info(req.sys_id);
        var app = new GlideRecord('sysapproval_approver');
        app.addQuery('sysapproval', req.sys_id);
        app.query();
        if (app.approver != current.u_level_1_approver) {
            gs.info(app.approver.getDisplayValue());
            app.state = 'cancelled';
            app.update();
 
        }
        var appr = new GlideRecord('sysapproval_approver');
        appr.initialize();
//appr.document_id = req.sys_id;
        appr.sysapproval = req.sys_id;
        appr.approver =current.u_level_1_approver;
        appr.state = 'requested';
        appr.insert();
    }
 
Best regards, 
Raj