BR is not working

Madhavi2
Tera Contributor

Hi All,

 

I am trying below code.

 

var usr=gs.getUserName();
gs.addInfoMessage("usr"+usr);
var pc=new GlideRecord("cmn_location");
pc.addQuery('name',usr);
pc.query();
if(pc.next())
{
var flag=pc.name;
gs.addInfoMessage(flag);
}
else
{
flag=111111;
gs.addInfoMessage(flag);
}
 
Even though logged in user is available in location table still it is going to else loop. Any inputs on what I'm missing. 
Same script is working in background script with hardcoded user name.
Thank you.
5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

Are you getting an expected value in your usr infoMessage?  And this value exactly matches the name field on a record on the location table, and the hardcoded value you are using in the background script?  You should always declare and initialize variables outside of any loops or if blocks.  As written, 'flag' is undefined in the else block

var usr=gs.getUserName();
var flag = '';
gs.addInfoMessage("usr = "+usr);
var pc=new GlideRecord("cmn_location");
pc.addQuery('name', usr);
pc.query();
if (pc.next()) {
    flag=pc.name;
    gs.addInfoMessage('if ' + flag);
} else {
    flag='111111';
    gs.addInfoMessage('else ' + flag);
}

 

Hi @Brad Bowman  ,

Yes, I am getting the expected value in usr info message. And the usr is available in location table as well. 

I have tried with your code as well. Still it is going else part.

 

In bg script it is working correctly.

 

Madhavi2_0-1710264377936.png

 

In form it is going to else.

Madhavi2_1-1710264420129.png

 

 

If anything I would expect the bg script to not work as the number 2000246 could be seen as not equal to the text value '2000246'.  If you hardcode the BR the same way instead of getUserName does it find the location record?

Sumanth16
Kilo Patron

Hi @Madhavi2 ,

 

pc.addQuery('name',usr); 
 
I see your querying name with user name. Is it custom field on the table?.
 
Name field does have location name in OOB version.
 
Add gs.info('records count'+pc.getRowCount()) to check how many records are querying with your query and run in scripts-background.
If you are running this script need to change current object to access field values.
 
var usr=gs.getUserName();
var flag=' ';
gs.addInfoMessage("usr"+usr);
var pc=new GlideRecord("cmn_location");
pc.addQuery('name',usr);//check this query 
pc.query();
if(pc.next())//if you are running for multiple records change to while
{
flag=pc.name;
gs.addInfoMessage(flag);
}
else
{
flag=111111;
gs.addInfoMessage(flag);
}
 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda