Better way to write this script instead of Else If Statement

psyherin
Kilo Sage

Hi ,

I'm new to scripting and having trouble wrapping my head around this one. If there is a better way to script it instead of using ELSE IF

I am trying to restrict records using a before query business rule.

 

var qu = '';

if(gs.getUser().hasRole("admin") || gs.getUser().isMemberOf('287ee6fea9fe198100ada7950d0b1b73')) {
qu = current.addEncodedQuery("");
}
else if (gs.getUser().hasRole("itil") && !gs.getUser().isMemberOf('287ee6fea9fe198100ada7950d0b1b73')) {
qu = current.addEncodedQuery('cat_item!=c0c5b2db4fbf4200086eeed18110c718');
}
else if (gs.getUser().hasRole("itil") && !gs.getUser().isMemberOf('123ee6fea9fe198100ada7950d0b1b73')) {
qu = current.addEncodedQuery('cat_item!=c0c5b2db4fbf4200086eeed18110c718');
}
else if (gs.getUser().hasRole("itil") && !gs.getUser().isMemberOf('789aaafea9fe198132ada7950d0b1b73')) {
qu = current.addEncodedQuery('cat_item!=c0c5b2db4fbf4200086eeed18110c718');
}

return;
1 ACCEPTED SOLUTION

Got it. OR'ing the isMemberOf's into a single line with the itil role check should work well for you.

View solution in original post

9 REPLIES 9

Yes, that should work just fine.

when i try changing the script as above (with multiple groups) the end user is unable to see the RITM in Service Portal 

(function executeRule(current, previous /*null when async*/ ) {
    var qu = '';

    if (gs.getUser().hasRole("admin") || gs.getUser().isMemberOf('287ee6fea9fe198100ada7950d0b1b73') || gs.getUser().isMemberOf('287ebd7da9fe198100f92cc8d1d2154e')) {
        qu = current.addEncodedQuery("");
    } else if (gs.getUser().hasRole("itil") && !gs.getUser().isMemberOf('287ee6fea9fe198100ada7950d0b1b73') || !gs.getUser().isMemberOf('287ebd7da9fe198100f92cc8d1d2154e')) {
        qu = current.addEncodedQuery('cat_item!=c0c5b2db4fbf4200086eeed18110c718');
    } else if (!gs.getUser().hasRole("snc_external")) {
        qu = current.addEncodedQuery("cat_item!=c0c5b2db4fbf4200086eeed18110c718^NQcat_item=c0c5b2db4fbf4200086eeed18110c718^request.requested_for=" + gs.getUserID());

    }

})(current, previous);

 

what am i doing wrong here ?  as the below code without additional group the end user is able to see the record in Service Portal

(function executeRule(current, previous /*null when async*/) {
	var qu = '';
   
	if(gs.getUser().hasRole("admin") || gs.getUser().isMemberOf('287ee6fea9fe198100ada7950d0b1b73')) { 
		qu = current.addEncodedQuery("");
	} 
	else if (gs.getUser().hasRole("itil") && !gs.getUser().isMemberOf('287ee6fea9fe198100ada7950d0b1b73')) {
		qu = current.addEncodedQuery('cat_item!=c0c5b2db4fbf4200086eeed18110c718');
	}
	else if (!gs.getUser().hasRole("snc_external")) {
        qu = current.addEncodedQuery("cat_item!=c0c5b2db4fbf4200086eeed18110c718^NQcat_item=c0c5b2db4fbf4200086eeed18110c718^request.requested_for=" + gs.getUserID());
    
    }

})(current, previous);

 

Try wrapping the OR portion into its own parentheses.

else if (gs.getUser().hasRole("itil") && (!gs.getUser().isMemberOf('287ee6fea9fe198100ada7950d0b1b73') || !gs.getUser().isMemberOf('287ebd7da9fe198100f92cc8d1d2154e')))

Thank you @Logan Poynter that worked. much appreciated. 

Voona Rohila
Kilo Patron
Kilo Patron

Hi Herin, Remove multiple if else and just use Or condition as Logan suggested and remove return statement from your code.

https://docs.servicenow.com/bundle/sandiego-application-development/page/script/business-rules/conce...


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP