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.

AddOrCondition is not giving expected result

Dinesh90
Tera Contributor

Hello

This is regarding a below script which is not working as expected.

Pls let me know the issue in below script , why it is not performing the AddorCondition line.

I have tried running it in BG , but outcome is "cannot convert null to an object" - on line addorCondition it is giving error.

var result;
var ritmObj = {};
ritmObj.user = [];
ritmObj.license = [];

var grAlm = new GlideRecord('xyz');
var grAlm1 = grAlm.addQuery('licensed_by.sys_id=4f9e91df971f6d507fbb77771153af46'+ '^assigned_to=' + '429e96b5dba5b0d4e65aa90b8a9619a3');
grAlm1.addOrCondition('licensed_by.sys_id=d0ad5ddb971f6d507fbb77771153af2f' + '^assigned_to=' + '429e96b5dba5b0d4e65aa90b8a9619a3');
grAlm.setLimit(1);
grAlm.query();
if (grAlm.next()) {
ritmObj.license = 'true';
} else {
ritmObj.license = 'false';
}
result = JSON.stringify(ritmObj);
gs.log('result is'+result);

 

Dinesh90_0-1686769225755.png

 

1 ACCEPTED SOLUTION

MahaAgineo
Tera Guru

Try this:

var result;
var ritmObj = {};
ritmObj.user = [];
ritmObj.license = [];

var grAlm = new GlideRecord('xyz');
grAlm.addEncodedQuery('licensed_by.sys_id=4f9e91df971f6d507fbb77771153af46'+ '^assigned_to=' + '429e96b5dba5b0d4e65aa90b8a9619a3^NQlicensed_by.sys_id=d0ad5ddb971f6d507fbb77771153af2f' + '^assigned_to=' + '429e96b5dba5b0d4e65aa90b8a9619a3');
grAlm.setLimit(1);
grAlm.query();
if (grAlm.next()) {
ritmObj.license = 'true';
} else {
ritmObj.license = 'false';
}
result = JSON.stringify(ritmObj);
gs.log('result is'+result);

View solution in original post

2 REPLIES 2

Karan Chhabra6
Mega Sage

Hi @Dinesh90 ,

 

addOrCondition accepts 2 or 3 parameters, first is the field name, second is the operator and third is the value which is an object

KaranChhabra6_0-1686769681704.png

 

So, in this line:

grAlm1.addOrCondition('licensed_by.sys_id=d0ad5ddb971f6d507fbb77771153af2f' + '^assigned_to=' + '429e96b5dba5b0d4e65aa90b8a9619a3');

The javascript parser is treating the second and third parameter as null, hence you are getting that error

 

This looks more like an encoded query, I would suggest you to use addEncodedQuery();

You can build your query from the list view and the copy the query and add it inside the addEncodedQuery() method

Refer to this doc: https://developer.servicenow.com/blog.do?p=/post/training-encoded-query/

 

If my answer has helped with your question, pelase mark it as correct and helpful

 

Thanks!

 

 

MahaAgineo
Tera Guru

Try this:

var result;
var ritmObj = {};
ritmObj.user = [];
ritmObj.license = [];

var grAlm = new GlideRecord('xyz');
grAlm.addEncodedQuery('licensed_by.sys_id=4f9e91df971f6d507fbb77771153af46'+ '^assigned_to=' + '429e96b5dba5b0d4e65aa90b8a9619a3^NQlicensed_by.sys_id=d0ad5ddb971f6d507fbb77771153af2f' + '^assigned_to=' + '429e96b5dba5b0d4e65aa90b8a9619a3');
grAlm.setLimit(1);
grAlm.query();
if (grAlm.next()) {
ritmObj.license = 'true';
} else {
ritmObj.license = 'false';
}
result = JSON.stringify(ritmObj);
gs.log('result is'+result);