The CreatorCon Call for Content is officially open! Get started here.

Function is not working in Scheduled Job

Amrutha K V
Tera Contributor

Hi Team,

I am using the below script for removing inactive users who is updated on last week from knowledge base table.
It is working without using function. But our requirement is to use the Knowledgebase function for removal of inactive users from kb table separately by calling the same inActiveUsers array inside the function. I tried the below script with function but it's not working. Could you please help me. Thank you!!


Note: We have to remove the inactive users from other tables(Assignment Data Lookups,Report,Scheduled Email of Report,Notification) also by mentioning different function for each table.

 

var inActiveUsers = [];
var gr1 = new GlideRecord('sys_user');
gr1.addEncodedQuery('active=false^sys_updated_onONLast week@javascript:gs.beginningOfLastWeek()@javascript:gs.endOfLastWeek()');
gr1.query();
while (gr1.next()) {
    inActiveUsers.push(gr1.sys_id.toString());
}

function knowledgebase(inActiveUsers) {  ------->1:This function is not working for me.
    gs.log("success111");
    if (inActiveUsers) {
        gs.log("success1");
        for (var i = 0; i < inActiveUsers.length; i++) {
            gs.info("inActiveUser[i]" + inActiveUsers[i]);
            var kbase = new GlideRecord('kb_knowledge_base');
            kbase.addEncodedQuery('kb_managersLIKE' + inActiveUsers[i]);
            kbase.query();
            var mgr = [];
            while (kbase.next()) {
                mgr = kbase.kb_managers.toString().split(',');
                gs.log("kbase.kb_managers" + kbase.kb_managers);
                for (i = 0; i < inActiveUsers.length; i++) {
                    for (j = 0; j < mgr.length; j++) {
                        if (mgr[j] == inActiveUsers[i]) {
                            var remove = mgr.indexOf(mgr[j]); // get the position of the element to be removed
                            mgr.splice(remove, 1); // remove the element from the array

                        }
                    }
                }
                kbase.kb_managers = mgr.toString();
                gs.log("BalanceAfterRemoval" + kbase.kb_managers);
                kbase.autoSysFields(false);
                kbase.update();
            }
        }
    }
}
4 ACCEPTED SOLUTIONS

Bert_c1
Kilo Patron

Hi Amrutha,

 

I don't see where you call the function in the script.

 

add "knowledgebase(inActiveUsers);" After the while loop.

View solution in original post

Harish Bainsla
Kilo Patron
Kilo Patron

var inActiveUsers = [];
var gr1 = new GlideRecord('sys_user');
gr1.addEncodedQuery('active=false^sys_updated_onONLast week@javascript&colon;gs.beginningOfLastWeek()@javascript&colon;gs.endOfLastWeek()');
gr1.query();
while (gr1.next()) {
inActiveUsers.push(gr1.sys_id.toString());
}

function knowledgebase(inActiveUsers) {
gs.log("success111");
if (inActiveUsers) {
gs.log("success1");
for (var i = 0; i < inActiveUsers.length; i++) {
gs.info("inActiveUser[i]" + inActiveUsers[i]);
var kbase = new GlideRecord('kb_knowledge_base');
kbase.addQuery('kb_managers', 'LIKE', inActiveUsers[i]);
kbase.query();
while (kbase.next()) {
var mgr = kbase.kb_managers.toString().split(',');
gs.log("kbase.kb_managers" + kbase.kb_managers);
for (var j = 0; j < mgr.length; j++) {
if (mgr[j] == inActiveUsers[i]) {
var remove = mgr.indexOf(mgr[j]); // get the position of the element to be removed
mgr.splice(remove, 1); // remove the element from the array
}
}
kbase.kb_managers = mgr.toString();
gs.log("BalanceAfterRemoval" + kbase.kb_managers);
kbase.autoSysFields(false);
kbase.update();
}
}
}
}

// Call the knowledgebase function with the inActiveUsers array
knowledgebase(inActiveUsers);

View solution in original post

Harish Bainsla
Kilo Patron
Kilo Patron

if my code help you please accept solution

View solution in original post

ok sure but please accept solution if my code help you and for another one post question again i try to give answer

 

View solution in original post

7 REPLIES 7

ok sure but please accept solution if my code help you and for another one post question again i try to give answer

 

Okay Sure I will. Thank you!! 

Harish Bainsla
Kilo Patron
Kilo Patron

if my code help you please accept solution