Business rule - Encoded Query

PawanKumarR
Tera Contributor

Hi All,

 

I have created a system properties "a_b_c" where i have stored three sys ids of a catalog in form of a,b,c.

now this i have to use in business rule. when my catalog is any of this (a or b or c). then do xyz.

i have to put this sys property a_b_c in Encoded Query of a BR.
i have used but its not selecting that three catalog ids.

script:

var sysIds = gs.getProperty('a_b_c').split(',');

var encodedQuery = ' ';
            for (var i = 0; i < sysIds.length; i++) {
                if (i > 0) {
                    encodedQuery += '^OR';
                }
                encodedQuery += 'cat_item=' + sysIds[i];
            }

var ritmGr = new GlideRecord('sc_req_item');
            ritmGr.addQuery("opened_by", current.u_owner_id);
            ritmGr.addEncodedQuery(encodedQuery);
            ritmGr.addActiveQuery();
            ritmGr.query();
            if (ritmGr.next()) {

1 ACCEPTED SOLUTION

J Siva
Tera Sage

Hi @PawanKumarR 

Try below script..

var sysIds = gs.getProperty('a_b_c').toString();

var encodedQuery = 'cat_itemIN' + sysIds;
var ritmGr = new GlideRecord('sc_req_item');
ritmGr.addQuery("opened_by", current.getValue('u_owner_id')); 
ritmGr.addEncodedQuery(encodedQuery);
ritmGr.addActiveQuery();
ritmGr.query();
while (ritmGr.next()) {
    //YOUR LOGIC HERE
}

Hope this helps .

Regards,

Siva

View solution in original post

15 REPLIES 15

Bert_c1
Kilo Patron

Seems you need to debug your script. check the results of the query 

gs.info('My script: query of sys_req_item found ' + ritmGr.getRowCount() + ' records for query: ' + encodedQuery);

before your 'if(ritmGr.next())' statement. and see what you get in the Script Log Statements module.

Kieran Anson
Kilo Patron

From a performance point of view, you can use the "IN" operator rather than OR. OR conditions cause additional database resources.

//getProperty allows for a second parameter
//to set a default value
var scCatItemSysIds = gs.getProperty('a_b_c', '');

var ritmGr = new GlideRecord('sc_req_item');
ritmGr.addQuery("opened_by", current.u_owner_id);
ritmGr.addQuery('cat_item' , 'IN' , scCatItemSysIds);
ritmGr.addActiveQuery();
//use this if you're going to use "if" rather than "while" when looking at record
//ritmGr.setLimit(1);
ritmGr.query();

not working

The value of the sys_properties record is a comma-separated list of sys_id values. A BR that runs Before for Query would have:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	if (!(gs.hasRole("admin") || gs.hasRole("user_admin")) && gs.getSession().isInteractive()) {
		current.addQuery('cat_item', 'IN', gs.getProperty('a_b_c'));
	}

})(current, previous);

adjust role check as desired so not all users are limited in what they see.

Tried with above too but not working. below is the full code.

 if (current.u_xyz) {
updateWorkflowStatus();
 
        function updateWorkflowStatus() {
       
var sysIds = gs.getProperty('a_b_c').split(',');

 

var encodedQuery = ' ';
            for (var i = 0; i < sysIds.length; i++) {
                if (i > 0) {
                    encodedQuery += '^OR';
                }
                encodedQuery += 'cat_item=' + sysIds[i];
            }

 

 

var ritmGr = new GlideRecord('sc_req_item');
            ritmGr.addQuery("opened_by", current.u_owner_id);
            ritmGr.addEncodedQuery(encodedQuery);
            ritmGr.addActiveQuery();
            ritmGr.query();
            if (ritmGr.next()) {
                var sctaskGr = new GlideRecord('sc_task');
                sctaskGr.addQuery("request_item", ritmGr.sys_id);
                sctaskGr.addQuery("order", 100);
                sctaskGr.addActiveQuery();
                sctaskGr.query();
                if (sctaskGr.next()) {
                    new Workflow().broadcastEventToCurrentsContexts(sctaskGr, 'update', null);
                }
            }
        }
    }