Not able to store the value properly of a field in my variable after doing a glide record.

Rohit_Singh_19
Tera Contributor

Hi All/ @Ankur Bawiskar 

 

I am doing a Glide Record in question_answer table with my query. Now six times I am checking if question name == "abc" and question value is "true" then set my variable as true (these six question are of type true/false) and seventh if condition is question name = "xyz" then set my variable value = to the question's value. Now the problem is with variable where I am storing the value of question i.e. VAR 7. I am getting not an expected value, however I am getting a value as "True" of some other question (which is of true/false).

The VAR 7 is a choice filed reference to sys user Lang, so I should get value of a selected lang. What I think is problem is that the question value is getting from a previous record question.  

Please suggest any other alternative so that I can store the values properly and call my script include function.

 

var qa = new GlideRecord('question_answer');
        qa.addQuery('table_sys_id=' + target.sys_id + '^question.nameINvariable1,variable2,variable3,variable4,variable5,variable6,variable7');
        qa.query();
        while (qa.next()) {   
            if (qa.question.name == 'ABC' && qa.value == 'true') {
                var VAR1 = true;
            }
            if (qa.question.name == 'EFG' && qa.value == 'true') {
                var VAR2 = true;
            }
            if (qa.question.name == 'HIJ' && qa.value == 'true') {
                var VAR3 = true;
            }
            if (qa.question.name == 'KM0' && qa.value == 'true') {
                var VAR4 = true;
            }
            if (qa.question.name == 'NOP' && qa.value == 'true') {
                var VAR5= true;
            }
            if (qa.question.name == 'QRS' && qa.value == 'true') {
                var VAR6= true;
            }
            if (qa.question.name == 'XYZ') {
                var VAR7= qa.value;    
            }   
           
        }
        gs.info("latest ongoing: latest >>  " + VAR7);
        returnStatement = new scriptINCLUDE().SCRIPTINCLUDEFUNCTION(VAR1, VAR2, VAR3, VAR4, VAR5, VAR6, VAR7);

  

2 ACCEPTED SOLUTIONS

GlideFather
Tera Patron

Hi @Rohit_Singh_19,

first of all, have you been thinking of using the switch instead of ifs?

Something like this:

switch (qa.question.name) {

    case 'ABC':

        if (qa.value === 'true') {

            var VAR1 = true;

        }

        break;

    case 'EFG':

        if (qa.value === 'true') {

            var VAR2 = true;

        }

        break;

    case 'HIJ':

        if (qa.value === 'true') {

            var VAR3 = true;

        }

        break;

    case 'KM0':

        if (qa.value === 'true') {

            var VAR4 = true;

        }

        break;

    case 'NOP':

        if (qa.value === 'true') {

            var VAR5 = true;

        }

        break;

    case 'QRS':

        if (qa.value === 'true') {

            var VAR6 = true;

        }

        break;

    case 'XYZ':

        var VAR7 = qa.value;

        break;

}


for the Var 7 try to add ".toString()" >>>>> 

                var VAR7= qa.value.toString();    

 or eventually:

 

 

var correctValue = qa.value.toString();                
var VAR7= correctValue;    

 

And last idea - does not it miss the update function?

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Rohit_Singh_19 

My points

1) Initialize variable outside loop

2) use else if OR switch

3) if VAR7 is a reference after the loop fetch the display value using GlideRecord query
try this

var VAR1 = false, VAR2 = false, VAR3 = false, VAR4 = false, VAR5 = false, VAR6 = false, VAR7 = '';
var VAR7_display = ''; // For display value

var qa = new GlideRecord('question_answer');
qa.addQuery('table_sys_id', target.sys_id);
qa.addQuery('question.name', 'IN', 'ABC,EFG,HIJ,KM0,NOP,QRS,XYZ');
qa.query();

while (qa.next()) {
    var qName = qa.question.name + '';
    var qValue = qa.value + '';
    if (qName == 'ABC' && qValue == 'true') VAR1 = true;
    else if (qName == 'EFG' && qValue == 'true') VAR2 = true;
    else if (qName == 'HIJ' && qValue == 'true') VAR3 = true;
    else if (qName == 'KM0' && qValue == 'true') VAR4 = true;
    else if (qName == 'NOP' && qValue == 'true') VAR5 = true;
    else if (qName == 'QRS' && qValue == 'true') VAR6 = true;
    else if (qName == 'XYZ') VAR7 = qValue; // This is likely a sys_id if it's a reference/choice
}

// If VAR7 is a sys_id referencing sys_user (for language), get display value
if (VAR7) {
    var userGR = new GlideRecord('sys_user');
    if (userGR.get(VAR7)) {
        VAR7_display = userGR.language + ''; // or whatever field you need
    }
}

gs.info("VAR7 sys_id: " + VAR7);
gs.info("VAR7 display value: " + VAR7_display);

// Call your Script Include function with VAR1-VAR7 (use VAR7 or VAR7_display as needed)
returnStatement = new scriptINCLUDE().SCRIPTINCLUDEFUNCTION(VAR1, VAR2, VAR3, VAR4, VAR5, VAR6, VAR7_display);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

11 REPLIES 11

Hi @Ankur Bawiskar I have tried your approach but it's still giving me a true value instead of the language value. 

 

else if (qName == 'XYZ') VAR7 = qValue; // This is likely a sys_id if it's a reference/choice

This is not a sys_id. The value is the string format.

 

Variable type is Select Box, Choice Table User [sys_user], Choice Field Language.  

 

@Rohit_Singh_19 

then if that variable is taking choices and you want display value then query question_choice table with this value and get the choice label

then pass it to script include.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I am simply getting the value from the question_answer record, that's not the issue but the problem is it's value if getting changed to true when I am checking inside the while loop since I have to store other variable value as well depending on the question. If I am querying a single question answer record then I am getting the proper value but I don't want to use Glide Record two times.  

 

That's the solution I am looking for. Is there a way to do it with the help of Object?

 

P.S- I just want to store Variable value (which is a type select box, Choice table sys_user and Choice field Language). 

@Rohit_Singh_19 

why not add logs and see what comes for each GlideRecord?

Please use the script I shared above and add logs and share the results

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I just tested using qa.value.toStirng() in a switch statement and now it's getting me the expected value. Sorry i removed your code before you responded so did not able to debug as you mentioned.

 

Without using toString() does it always get an unexpected value as I was getting true value in place of language choice value? I did not understand this behavior of the system.