How to create a record when addEncodedQuery result is EMPTY (no records hit) in Business Rule?

Aki17
Kilo Guru

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

1 ACCEPTED SOLUTION

sekhar kurumoju
Mega Guru

Hai @Aki 

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.

View solution in original post

4 REPLIES 4

Sulabh Garg
Mega Sage
Mega Sage

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

Please Mark Correct/helpful, if applicable, Thanks!!
Regards
Sulabh Garg

Jaspal Singh
Mega Patron
Mega Patron

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);

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

sekhar kurumoju
Mega Guru

Hai @Aki 

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.