Issue with Query Business rule condition

Ak8977
Tera Expert

Could anyone help me to fix the following  before query BR.
I am having the following requirement.
if the user is from xyz group, the ticket contains a company field. If the company reg id is abc123.  then the user was able to see the record else he don't need to see those record.

var newQuery;
var userId = gs.getUserID();
var myGroups = gs.getMyGroups();

if((gs.getUser().isMemberOf('1054f30f1ba2b91484aa85d2604bcba9') {

if(current.company.reg_id =="abc123"){ //current record company reg_id, the reg_id is  in company table

     newQuery = current.addQuery("company.reg_id", 'abc123');
}

else if(current.company.reg_id =="xyz123"){ //current record company reg_id, the reg_id is  in company table

     newQuery = current.addQuery("company.reg_id", 'xyz123');
}

else {

    newQuery = current.addQuery("sys_id", '-1');

}

}

else {

    newQuery = current.addQuery("sys_id", '-1');

}

 

I written the above before queryBr  but the nested if condition and else if conditions are not working. can anyone help me on this.
thanks.

// I only need query BR not ACL

 

2 REPLIES 2

Maddysunil
Kilo Sage

@Ak8977 

  1. The first if condition is missing a closing parenthesis.
  2. You're using current without initializing it. You should use current only if you're writing a Business Rule on the table where the current record belongs to. If you're writing a Business Rule on a different table, you need to use new GlideRecord('table_name') to initialize a new GlideRecord object.

 

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

    var newQuery;
    var userId = gs.getUserID();
    var myGroups = gs.getMyGroups();

    if (gs.getUser().isMemberOf('1054f30f1ba2b91484aa85d2604bcba9')) { // Added missing closing parenthesis

        var companyGR = new GlideRecord('company');
        companyGR.addQuery('reg_id', current.getValue('company')); // Assuming 'company' is the reference field to the company table
        companyGR.query();

        if (companyGR.next()) {
            var regId = companyGR.getValue('reg_id');
            if (regId == 'abc123' || regId == 'xyz123') {
                newQuery = ''; // Do nothing, allow the query to proceed
            } else {
                newQuery = 'sys_id=-1'; // Exclude the record
            }
        } else {
            newQuery = 'sys_id=-1'; // Exclude the record if company record is not found
        }

    } else {
        newQuery = 'sys_id=-1'; // Exclude the record if user is not a member of the specified group
    }

    current.addEncodedQuery(newQuery);

})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Amit Pandey
Kilo Sage

Hi @Ak8977 

 

There are some syntax level issues in this existing script.

 

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

    var newQuery;
    var userId = gs.getUserID();
    var myGroups = gs.getMyGroups();

    if (gs.getUser().isMemberOf('1054f30f1ba2b91484aa85d2604bcba9')) { 

        if (current.getReference('company').reg_id == "abc123") {
            newQuery = current.addQuery("company.reg_id", 'abc123');
        } else if (current.getReference('company').reg_id == "xyz123") { 
            newQuery = current.addQuery("company.reg_id", 'xyz123');
        } else {
            newQuery = current.addQuery("sys_id", '-1');
        }

    } else {
        newQuery = current.addQuery("sys_id", '-1');
    }

})(current, previous);

 Please check this once.

 

Regards,

Amit