Calling System Property in Business Rule

Raksha6
Kilo Contributor

Hello,

I'm working on a Business Rule that functions as expected, however I need to figure out a way to remove the hard-coded sysIDs from the script. I'm thinking that storing the values in a system property and calling it from the BR would be best practice, but not sure how to incorporate this logic into my code. Any guidance would be greatly appreciated!

Business Rule:

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

    // Add your code here
    var control_owners = [];
    var unique_control_owners = [];
    var eventparm2 = "";
    var eventparm3 = "";
    var filter = "";
    //Check if user is Control Owner & Entity = IT Enterprise SW App or IT Telecom
    var gr = new GlideRecord('sn_compliance_control');
    gr.addEncodedQuery("state!=retired^key_control=true^profile=d41622692fbe81107fcc136a2799b6c1^ORprofile=9c1662692fbe81107fcc136a2799b61c^ownerLIKE" + current.sys_id);
    gr.query();
    while (gr.next()) {
        if (gr.u_admin_owner.toString().indexOf(",") > -1) {
            var temp = gr.u_admin_owner.toString().split(",");
            for (var i = 0; i < temp.length; i++) {
                control_owners.push(temp[i]);
            }
        } else {
            control_owners.push(gr.u_admin_owner.toString());
        }
    }
    unique_control_owners = new ArrayUtil().unique(control_owners);
    for (var j = 0; j < unique_control_owners.length; j++) {
        filter = "state!=retired^key_control=true^profile=d41622692fbe81107fcc136a2799b6c1^ORprofile=9c1662692fbe81107fcc136a2799b61c^ownerLIKE" + current.sys_id + "^u_admin_ownerLIKE" + unique_control_owners[j];
        eventparm2 = unique_control_owners[j] + "||" + filter;
        eventparm3 = filter + "||" + current.name.toString()+"||"+current.getTableName()+"||"+current.sys_id;
        gs.eventQueue("user.termination", current, unique_control_owners[j], eventparm2);
        gs.eventQueue("transfer.termination.records", current, unique_control_owners[j], eventparm3);
    }

    var executive_owners = [];
    var unique_executive_owners = [];
    //Check if user is Administrative Owner & Entity = IT Enterprise SW App or IT Telecom
    var gr2 = new GlideRecord('sn_compliance_control');
    gr2.addEncodedQuery("state!=retired^key_control=true^profile=d41622692fbe81107fcc136a2799b6c1^ORprofile=9c1662692fbe81107fcc136a2799b61c^u_admin_ownerLIKE" + current.sys_id);
    gr2.query();
    while (gr2.next()) {
        executive_owners.push(gr2.u_executive_owner.sys_id.toString());
    }
    unique_executive_owners = new ArrayUtil().unique(executive_owners);
    for (var k = 0; k < unique_executive_owners.length; k++) {
        filter = "state!=retired^key_control=true^profile=d41622692fbe81107fcc136a2799b6c1^ORprofile=9c1662692fbe81107fcc136a2799b61c^u_admin_ownerLIKE" + current.sys_id + "^u_executive_owner=" + unique_executive_owners[k];
        eventparm2 = unique_executive_owners[k] + "||" + filter;
        eventparm3 = filter + "||" + current.name.toString()+"||"+current.getTableName()+"||"+current.sys_id;
        gs.eventQueue("user.termination", current, unique_executive_owners[k], eventparm2);
        gs.eventQueue("transfer.termination.records", current, unique_executive_owners[k], eventparm3);
    }

    //Check if user is Executive Owner & Entity = IT Enterprise SW App or IT Telecom
    var gr3 = new GlideRecord('sn_compliance_control');
    gr3.addEncodedQuery("state!=retired^key_control=true^profile=d41622692fbe81107fcc136a2799b6c1^ORprofile=9c1662692fbe81107fcc136a2799b61c^u_executive_owner=" + current.sys_id);
    gr3.query();
    if (gr3.getRowCount() > 0) {
        filter = "state!=retired^key_control=true^profile=d41622692fbe81107fcc136a2799b6c1^ORprofile=9c1662692fbe81107fcc136a2799b61c^u_executive_owner=" + current.sys_id;
        eventparm2 = "Termination||" + filter + "||" + current.name.toString();
        eventparm3 = filter + "||" + current.name.toString()+"||"+current.getTableName()+"||"+current.sys_id;
        gs.eventQueue("executive.owner.termination", current, "TestEmailAddress@gmail.com", eventparm2);
        gs.eventQueue("transfer.terminaton.records", current, "TestEmailAddress@gmail.com", eventparm3);
    }

})(current, previous);​

 

1 ACCEPTED SOLUTION

Hi,

I'm unsure what you mean by "that didn't work", as what I provided does in fact work.

If you don't give us all the details, we can't really respond with a more precise answer for you.

With that said, if you have 10 sys_ids, you can store them in one property as JSON such as:

{"team_1":"sys_id_1","team_2":"sys_id_2"}

Then in your business rule script, you can use:

var obj = gs.getProperty('property_name');
obj = JSON.parse(obj);
gr.addEncodedQuery("state!=retired^key_control=true^profile=" + obj.team_1 +"^ORprofile=" + obj.team_2 + "^ownerLIKE" + current.sys_id);

Again, just examples, but you can take it from here.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

10 REPLIES 10

Hi Allen, Unfortunately that didn't work, but thank you for the response. I have a total of 10 sysIDs, but only used 2 in my script for testing. Ideally, I would like to have all 10 values stored in one system property, but I'm not sure if this can be done.

Hi,

You can call system property in BR and split it by comma. After that you have to use them by index position.

 

var propertyValue = gs.getProperty('property.name);
var valueArray = propertyValue.split(',');

var sys_id_1 = valueArray[1]; // enter the correct position
var sys_id_2 = valueArray[2];  // enter the correct position

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Hi,

You can refer my article:System Properties & its Usage

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Create a system property name like "profile.sys_ids" with comma delimited sys_id values.

e.g. 

d41622692fbe81107fcc136a2799b6c1,9c1662692fbe81107fcc136a2799b61c

Then in the script, do something like below.

var propertyValues = gs.getProperty('profile.sys_ids');
var fileSysIds = propertyValues.split(',');
        filter = "state!=retired^key_control=true^profile=" + profileSysIds[0] + "^ORprofile=" + profileSysIds[1] + "^ownerLIKE" + current.sys_id + "^u_admin_ownerLIKE" + unique_control_owners[j];

Hi,

I'm unsure what you mean by "that didn't work", as what I provided does in fact work.

If you don't give us all the details, we can't really respond with a more precise answer for you.

With that said, if you have 10 sys_ids, you can store them in one property as JSON such as:

{"team_1":"sys_id_1","team_2":"sys_id_2"}

Then in your business rule script, you can use:

var obj = gs.getProperty('property_name');
obj = JSON.parse(obj);
gr.addEncodedQuery("state!=retired^key_control=true^profile=" + obj.team_1 +"^ORprofile=" + obj.team_2 + "^ownerLIKE" + current.sys_id);

Again, just examples, but you can take it from here.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!