How do I get name of the view that submitted the form inside a business rule

jmweli
Kilo Expert

Hi ServiceNow Developers,

 

Is there a way to get the name of the current view inside a business rule?

 

Please advise.

 

Thanks,

Johannes

1 ACCEPTED SOLUTION

andrew_venables
ServiceNow Employee
ServiceNow Employee

Since Business Rules aren't really linked to the form that submitted them there is nothing OOTB that will get you this, however it should be pretty straightforward to create something:



First create a new field/column and add it to your form and all views. Something like u_view String(40). Remember to add it to each of the views for your table.


Second create a Client Script that runs onSubit for all views (global). In the Client script you should call:



function onSubmit() {


        g_form.setValue('u_view', getView());


}



getView() is a Client Side function that returns the name of the view as a string. You could also use an onLoad Client Script.



Then in your business rule current.u_view will contain the view name. Remember that the business rule might be fired by other business rules or non user/form submits and the Default View will show as an empty string "". You may also want to set the u_view field to be blank at the end of the Business Rule to avoid your code being executed when you don't want it.



Optionally you may want to create a UI Policy that hides the field from the user.


View solution in original post

3 REPLIES 3

andrew_venables
ServiceNow Employee
ServiceNow Employee

Since Business Rules aren't really linked to the form that submitted them there is nothing OOTB that will get you this, however it should be pretty straightforward to create something:



First create a new field/column and add it to your form and all views. Something like u_view String(40). Remember to add it to each of the views for your table.


Second create a Client Script that runs onSubit for all views (global). In the Client script you should call:



function onSubmit() {


        g_form.setValue('u_view', getView());


}



getView() is a Client Side function that returns the name of the view as a string. You could also use an onLoad Client Script.



Then in your business rule current.u_view will contain the view name. Remember that the business rule might be fired by other business rules or non user/form submits and the Default View will show as an empty string "". You may also want to set the u_view field to be blank at the end of the Business Rule to avoid your code being executed when you don't want it.



Optionally you may want to create a UI Policy that hides the field from the user.


Fantastic answer, Andrew!



You just can't get 'view' information at the server level directly without somehow feeding the business rule from something on the client side (like Andrew posted above).



Like++!


Hi Andrew,



I implemented it exactly the way you explained it and it's working correctly.



Thank you very much for a great answer and for your help