Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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?

_____
This reply is 100 % GlideFather and 0 % AI

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

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?

_____
This reply is 100 % GlideFather and 0 % AI

@Rohit_Singh_19 and BTW for the var 7 - do you only select existing values or do you create new ones as well?

_____
This reply is 100 % GlideFather and 0 % AI

Hi @GlideFather For var7 I want the existing value which it holds. I tried your approach it's working only when i am using  qa.value.toString(), without it's behaving same like my logic getting value as true. 

 

Also I have to use switch (qa.question.name.toString())  -> Without this switch was not working at all.

 

I will do few more rounds of testing and let you know the outcome. Thanks for your response.

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