Switch Statement in Business Rule

cc11
Tera Contributor

I have a business rule :

Runes Before Insert & Update with no additional condition.

I have a field called eventmessage (Choice type) on the form (its referred to in the script below).

I select a dropdown value from eventmessage field and save the form.

On this uddate, I need the business to run and take appropriate action based on the value selected.

The switch statements in the script below do not execute. Looking for help..  

Script:

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

//gs.addInfoMessage(current.eventmessage);

switch (current.eventmessage) {

case AssignCase:

gs.addInfoMessage("server side script - AssignCase event executed");

break;

case CalculateInvoiceTotals:

gs.addInfoMessage("server side script - CalculateInvoiceTotals event executed");

break;

case Failed:

gs.addInfoMessage("server side script - Failed event executed");

break;

default:

gs.addInfoMessage("server side script - default event executed");

}

})(current, previous);

1 ACCEPTED SOLUTION

That's correct, even single quotes will work when you use toString() in JavaScript, i just reproduced the issue and was going to reply but you found that. Great.



Please mark the answer correct so it will move out from unanswered list.


View solution in original post

8 REPLIES 8

Shishir Srivast
Mega Sage

Is eventmessage a correct field name?


Shishir Srivast
Mega Sage

Please have case values in quotes and try, like



case 'AssignCase'


case 'CalculateInvoiceTotals'


Karthik Reddy T
Kilo Sage

Hello Yogesh,



Please cross verify the event name. May be that was the issue.


Karthik Reddy T.
ServiceNow Commnunity MVP -2018 class.

cc11
Tera Contributor

Thank you Shishir, Karthik, I really appreciated your help.



Meanwhile, I was browsing the community posts and a suggestion from the post below worked for me..


Re: Switch case statement in a script



Here is what worked for me --



I created a variable before the Switch Statement


var x = current.eventmessage.toString();



Used the variable x in the Switch


Switch(x):{



Because the input parameter was converted to string, I added double quotes to the Case variables as follows


case "AssignCase"




Thank you again for your help,



Regards,


Yogesh