- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 02:33 AM
I created the following After/Insert Business Rule to create a record when addEncodedQuery result is empty.
After an import set table[u_application] record is created, search for [cmdb_ci_spkg] records which has name=current.u_productname AND version=current.version.
Then, if the query result is empty (there is no record hit), create a record in [cmdb_ci_spkg] table.
(function executeRule(current, previous /*null when async*/) {
var spk = new GlideRecord('cmdb_ci_spkg');
spk.addEncodedQuery('name=current.u_pruductname^version=current.version');
spk.query();
if (!spk.next()) {
spk.initialize();
spk.setValue('name', current.u_productname);
spk.setValue('version', current.u_version);
spk.insert();
}
})(current, previous);
This script worked when there is no record hit in the query, but also worked when there is already...
Could someone please revise the script to make it work only when there is no record in [cmdb_ci_spkg] table which has name=current.u_productname AND version=current.version.
Best Regards,
Aki
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 02:47 AM
Hai
you encode is not working because your using current Object use with in Quotes ('current.u_producation' & 'current. version') The format of encode query is wrong. so when addQuery() or encodeQuery() is wrong it is always empty.
so pls flow below format :
(function executeRule(current, previous /*null when async*/) {
var spk = new GlideRecord('cmdb_ci_spkg');
var poduction=current.u_pruductname;
var version=current.version;
var queryEnoced='name='+poduction+'^version='+version;
spk.addEncodedQuery(queryEnoced);
spk.query();
if (!spk.next()) {
spk.initialize();
spk.setValue('name', current.u_productname);
spk.setValue('version', current.u_version);
spk.insert();
}
})(current, previous);
Regards,
Sekhar.
Please mark answer correct/helpful based on impact.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 02:39 AM
Hello
You can have one more IF condition below this line -- if (!spk.next()) {
If (spk.getRowCount()==0)
{
spk.initialize();
spk.setValue('name', current.u_productname);
spk.setValue('version', current.u_version);
spk.insert(); }
}
Please Mark ✅ Correct/helpful, if applicable, Thanks!!
Regards
Sulabh Garg
Regards
Sulabh Garg

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 02:42 AM
Can you try below
(function executeRule(current, previous /*null when async*/) {
var spk = new GlideRecord('cmdb_ci_spkg');
spk.addEncodedQuery('name=current.u_pruductname^version=current.version');
spk.query();
if (!spk.next()) {
var spk1=new GlideRecord('cmdb_ci_spkg');
spk1.initialize();
spk1.setValue('name', current.u_productname);
spk1.setValue('version', current.u_version);
spk1.insert();
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 02:45 AM
Hi,
something like this
use correct query
(function executeRule(current, previous /*null when async*/) {
var spk = new GlideRecord('cmdb_ci_spkg');
spk.addEncodedQuery('name=' + current.u_pruductname + '^version=' + current.version');
spk.query();
if (!spk.hasNext()) {
spk.initialize();
spk.setValue('name', current.u_productname);
spk.setValue('version', current.u_version);
spk.insert();
}
})(current, previous);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 02:47 AM
Hai
you encode is not working because your using current Object use with in Quotes ('current.u_producation' & 'current. version') The format of encode query is wrong. so when addQuery() or encodeQuery() is wrong it is always empty.
so pls flow below format :
(function executeRule(current, previous /*null when async*/) {
var spk = new GlideRecord('cmdb_ci_spkg');
var poduction=current.u_pruductname;
var version=current.version;
var queryEnoced='name='+poduction+'^version='+version;
spk.addEncodedQuery(queryEnoced);
spk.query();
if (!spk.next()) {
spk.initialize();
spk.setValue('name', current.u_productname);
spk.setValue('version', current.u_version);
spk.insert();
}
})(current, previous);
Regards,
Sekhar.
Please mark answer correct/helpful based on impact.