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

can you use a script variable in a dot walk?

randrews
Tera Guru

ok this may seem silly but i want to know if anyone has figured out how to use a variable in the middle of the . sequence to set/query a field...

so for example say i have the following script...

var query_string = 'string';

var update_var = 'u_variable1';

update_records(query_string, update_var);

update_var = 'u_variable2';

update_records(query_string, update_var);

function update_records(qry,var1){

        rec = new GlideRecord('some_table');

      rec.addEncodedQuery('qry');

      rec.query();

      while(rec.next()){

                rec.var1 = false;         //how do i get this to look at u_variable1 or variable 2 depending on the value of update_var?

                  rec.update();

      }

}

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Doug,



I believe you can use something like the following:



rec[var1] = false;


View solution in original post

3 REPLIES 3

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Doug,



I believe you can use something like the following:



rec[var1] = false;


Sweet thanks works like a charm and keeps me from having to call multiple functions that are all doing the same thing to update multiple fields.


How about when you need to go deeper in the dotWalk?

 

//variables definition
var bioQueryStr = 'sys_updated_onNOTONLast 6 months@javascript:gs.beginningOfLast6Months()@javascript:gs.endOfLast6Months()^u_user.active=true';
var arr = [];
 
//function call
var userBio1 = fillArr(bioQueryStr, 'u_user', 'u_user.department.sys_id');
 
//function definition
function fillArr(nameQueryStr, nameGroupByStr, nameRef){
    var arr = [];
    var bioGR = new GlideAggregate("u_cv_portal_bio");
    bioGR.addEncodedQuery(nameQueryStr);
    bioGR.groupBy(nameGroupByStr);
    bioGR.query();
    while (bioGR.next() && bioGR[nameRef].getValue()) {
        arr.push(bioGR[nameRef].getValue());
    }
    return arr;
}
 
 
 
 
this doesn't work, found a workaround: 
 
 
 
 
//variables definition
var bioQueryStr = 'sys_updated_onNOTONLast 6 months@javascript:gs.beginningOfLast6Months()@javascript:gs.endOfLast6Months()^u_user.active=true';
var arr = [];
 
//function call
var userBio1 = fillArr(bioQueryStr, 'u_user', 'u_user', 'sys_id');
 
//function definition
function fillArr(nameQueryStr, nameGroupByStr, nameRef, nameRef2){
    var arr = [];
    var bioGR = new GlideAggregate("u_cv_portal_bio");
    bioGR.addEncodedQuery(nameQueryStr);
    bioGR.groupBy(nameGroupByStr);
    bioGR.query();
    while (bioGR.next() && bioGR[nameRef].getValue()) {
        if(nameRef2){
            arr.push(bioGR[nameRef][nameRef2].getValue());
        }else{
            arr.push(bioGR[nameRef].getValue());
        }
    }
    return arr;
}
 
The question is, can it be done more cleanly, like in the first example? What am I missing?