Need Help with Business Rule condition

geet
Tera Guru

Hi,

 

geet_0-1695271902387.png

I have a BR where I am trying to abort the submission if user has selected Yes is approval required but has not selected one of the approval.
User should either select user approval or group approval. If none is selected it will abort action.
Here is the code. BR is acting strange as I think my condition is not correct. It is only allowing submission when both checkboxes are checked.

Please guide.

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

	// Add your code here
	
	var ApprovalRequired = current.approval_required;
	gs.info(ApprovalRequired);
	var userApproval = current.user_approval;
	var groupApproval = current.group_approval;
	if(ApprovalRequired == 'yes' && (userApproval == '' || groupApproval == '')){
		
		
			gs.addErrorMessage("Please select either User or Group Approval when Approval required is selected as Yes");
			current.setAbortAction(true);
		
	}

})(current, previous);

 

1 ACCEPTED SOLUTION

RAMANA MURTHY G
Mega Sage
Mega Sage

Hello @geet ,

 

You need to change 

 

 

if(ApprovalRequired == 'yes' && (userApproval == '' || groupApproval == '')){

 

this statement as below

 

if(approval == 'Yes' && !(userApprove == true || groupApprove == true)){

 

 

You can achieve the same without using a script, please find below screenshots

 

RAMANAMURTHYG_1-1695274306724.png

 

 

RAMANAMURTHYG_0-1695274247377.png

 

 

Please mark my answer helpful, if it helps you

Thank you

 

 

 

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@geet 

script looks good.

are you checking correct choice value for yes?

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

RAMANA MURTHY G
Mega Sage
Mega Sage

Hello @geet ,

 

You need to change 

 

 

if(ApprovalRequired == 'yes' && (userApproval == '' || groupApproval == '')){

 

this statement as below

 

if(approval == 'Yes' && !(userApprove == true || groupApprove == true)){

 

 

You can achieve the same without using a script, please find below screenshots

 

RAMANAMURTHYG_1-1695274306724.png

 

 

RAMANAMURTHYG_0-1695274247377.png

 

 

Please mark my answer helpful, if it helps you

Thank you

 

 

 

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

Vishal Birajdar
Giga Sage

Hi @geet 

 

I think your code is Ok.

Check backend value of Yes/No type is "Yes" or "No" / 'yes' or 'no'

For checkbox its "true" or "false"

Have updated if condition :

if(ApprovalRequired == 'Yes' && (userApproval == 'false' || groupApproval == 'false'))

 

 

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

	// Add your code here
	
	var ApprovalRequired = current.approval_required;
	gs.info(ApprovalRequired);
	var userApproval = current.user_approval;
	var groupApproval = current.group_approval;
	if(ApprovalRequired == 'Yes' && (userApproval == 'false' || groupApproval == 'false')){
		
		
			gs.addErrorMessage("Please select either User or Group Approval when Approval required is selected as Yes");
			current.setAbortAction(true);
		
	}

})(current, previous);

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

_gaurav_S
Tera Expert

Hi @geet ,
User Approval and Group Approval stores values as Boolean fields, So make these changes in your Condition:

if(ApprovalRequired == 'yes' && (userApproval == false || groupApproval == false))