get variable values in array

Shivani29
Mega Guru

Hi,

I have a requirement, where I need to find if extension ITSR is raised for the user, then need to check if Leaver is raised. In case raised, then it should be cancelled. I wrote below code to get the variable value in array but it is not getting any value. 

var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('cat_item=89f0fe701b1af300e18764606e4bcb8d^state=1^ORstate=2');
gr.query();
while(gr.next()){
gs.log("inside while");
//usr = gr.variables.u_windows_id;
usr.push=gr.getValue(gr.variables.u_windows_id);}
gs.log('usr1 '+usr);

 

Kindly help!!

Regards,

Shivani

1 ACCEPTED SOLUTION

@Shivani29  - I've tried to simplify the script a bit, please refer to the script below, it will only enter the if block if the value in usr array is same as the value in loginID array

var usr = [];
var loginID = [];
var ritmGR1 = new GlideRecord('sc_req_item');
ritmrGR1.addEncodedQuery('cat_item=89f0fe701b1af300e18764606e4bcb8d^state=1^ORstate=2');
ritmGR1.query();
while (ritmrGR1.next()) {
    gs.log("inside while");
    usr.push(ritmGR1.variables.u_windows_id.getDisplayValue());
}
gs.log('usr1 ' + usr);
var ritmGR2 = new GlideRecord('sc_req_item');
ritmGR2.addEncodedQuery('cat_item=65c6d644378c17c01ecacea954990ea5^state=1^ORstate=2');
ritmGR2.query();
gs.log('count ' + ritmGR2.getRowCount());
while (ritmGR2.next()) {
    gs.log("inside ls while");
    loginID.push(ritmGR2.variables.u_employee_login_id.getDisplayValue());
}
gs.log('loginId1 ' + loginID);
for (var i = 0; i < loginID.length; i++) {
    for (var x = 0; x < usr.length; x++) {
        gs.log('x n i ' + x[0] + i[0]);
        if (usr[x] == loginId[i]) {
            gs.log('inside ls if');
            ritmGR2.setValue('state', 4);
        }
    }
}

 

View solution in original post

11 REPLIES 11

Nitesh A
Tera Expert

Hello Shivani29

Try using below script

 

var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('cat_item=89f0fe701b1af300e18764606e4bcb8d^state=1^ORstate=2');
gr.query();
gs.print(gr.getRowCount());
var usr = [];
while(gr.next()){
gs.log("inside while");
usr.push(gr.variables.u_windows_id);
}
gs.log('usr1 :'+usr);

 

Please mark this response as correct or helpful if it assisted you with your question.

Regards,
Nitesh

Hi Nitesh,

 

Thanks for your reply. I tried this already but it did not work. log was empty. No value stored in usr.

 

Regards,

Shivani

Can you please try now. 
I have updated the script

Please mark this response as correct or helpful if it assisted you with your question.

Regards,
Nitesh

Karan Chhabra6
Mega Sage
Mega Sage

Hi @Shivani29 ,

 

Please refer to the script below

var usr = [];
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('cat_item=89f0fe701b1af300e18764606e4bcb8d^state=1^ORstate=2');
gr.query();
while(gr.next()){
gs.log("inside while");
usr.push(gr.variables.u_windows_id);
}
gs.log('usr1 '+usr);

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks!