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.

GlideRecord Query returns nothing

danielschmidt
Kilo Expert

Hello guys,

thanks for the last two posts where i did get so much help. I have another problem now by doing my first gliderecord.

I'm at this point so far (Client Script):

function onLoad() {

//Type appropriate comment here, and begin script below

var user = g_user.getFullName();

alert(user);

var a = new Array("Test 101", "Test 102");

var b = new Array("101", "102");

var qString = '';

for(var i=0; i<a.length; i++) {

var ir = new GlideRecord('sys_user_grmember');

ir.addQuery('group', a[i]);

ir.addQuery('user', user);

ir.query();

alert(a[i]);

if(ir.hasNext() == 'true'){

if (qString.length > 0) {

qString = "u_field=" + b[i];

} else {

qString += "^ORu_field=" + b[i];

}

} else {

qString = 'Test'; //just for testing

}

}

g_form.setValue('u_field2', qString);

}

I've put in some alerts to get the values of the variables at each point. And they do have the correct values at all time. But i didn't get any of the strings to my variable qString. I did manually check for the sys_user_grmember table and filtered for the user name (System Administrator) and the first group. I found one record. So there should be a result into the qString. Can somebody tell me what i did wrong there?

If you need further information, like always, feel free to ask. I'm really tired so please excuse me if i forgot something.

Greetings and all the best /Daniel

1 ACCEPTED SOLUTION

sachin_namjoshi
Kilo Patron
Kilo Patron

Hi Daniel,



I modified your client script like below



function onLoad() {  


//Type appropriate comment here, and begin script below  


var user = g_user.userID;  


alert(user);  


var a = new Array("Test 101", "Test 102");   // update sys_id of group


var b = new Array("101", "102");  


var qString = '';  


 


for(var i=0; i<a.length; i++) {  


var ir = new GlideRecord('sys_user_grmember');  


ir.addQuery('group', a[i]);  


ir.addQuery('user', user);  


ir.query();  


alert(a[i]);  


if(ir.hasNext() == 'true'){  


if (qString.length > 0) {  


qString = "u_field=" + b[i];  


} else {  


qString += "^ORu_field=" + b[i];  


}  


} else {  


qString = 'Test'; //just for testing  


}  


}  


g_form.setValue('u_field2', qString);  


}  



Regards,


Sachin


View solution in original post

12 REPLIES 12

sachin_namjoshi
Kilo Patron
Kilo Patron

Hi Daniel,



I modified your client script like below



function onLoad() {  


//Type appropriate comment here, and begin script below  


var user = g_user.userID;  


alert(user);  


var a = new Array("Test 101", "Test 102");   // update sys_id of group


var b = new Array("101", "102");  


var qString = '';  


 


for(var i=0; i<a.length; i++) {  


var ir = new GlideRecord('sys_user_grmember');  


ir.addQuery('group', a[i]);  


ir.addQuery('user', user);  


ir.query();  


alert(a[i]);  


if(ir.hasNext() == 'true'){  


if (qString.length > 0) {  


qString = "u_field=" + b[i];  


} else {  


qString += "^ORu_field=" + b[i];  


}  


} else {  


qString = 'Test'; //just for testing  


}  


}  


g_form.setValue('u_field2', qString);  


}  



Regards,


Sachin


Thanks to you guys. I did Change the Group Name to the sys_id. I had the userID at first for the user, but that didn't worked. Now i changed that again and nothing happens.


i added another alert after "if(ir.hasNext() == 'true'{" but i don't get this alert message at any Point of the code.



Does anyone has an idea?


Dhravesh Murpan
Mega Expert
  1. function onLoad() {  
  2. //Type appropriate comment here, and begin script below  
  3. var user = g_user.userID; // corrected   require sysID
  4. alert(user);  
  5. var a = new Array("Test 101", "Test 102");   // use the sys_id for the groups
  6. var b = new Array("101", "102");  
  7. var qString = '';  
  8.  
  9. for(var i=0; i<a.length; i++) {  
  10. var ir = new GlideRecord('sys_user_grmember');  
  11. ir.addQuery('group', a[i]);   // group is reference field
  12. ir.addQuery('user', user);   // user is reference field
  13. ir.query();  
  14. alert(a[i]);  
  15. if(ir.hasNext()){     // corrected
  16. if (qString.length > 0) {  
  17. qString = "u_field=" + b[i];  
  18. }
  19. else {  
  20. qString += "^ORu_field=" + b[i];  
  21. }  
  22. }
  23. else {  
  24. qString = 'Test'; //just for testing  
  25. }  
  26. }  
  27. g_form.setValue('u_field2', qString);  
  28. }  

Rajesh T
Giga Expert

Declare qString as array type since condition is checking length (Length method won't be work for String type).


var qString=[];


Add values to qString using push method.



or


If you want use it as - var qString='';   then modify the condition that qString!='' or qString=='' instead of use length .



Thanks,


Rajesh T