How to redirect with a Display Business rule before the form loads?

tim_harris
Giga Expert

Has anyone here used a Display Business Rule to redirect before a form load? I am running into issues. I create the business rule to only run on the "kb_knowledge" table but once it is active every link in ServiceNow is redirecting via the Display BR. Very strange… Here is the code:

function onDisplay(current, g_scratchpad) {

      //This function will be automatically called when this rule is processed.

      var url = RP.getReferringURL();

      var kb_id = url.match(/sysparm_collectionID=(\w+)&?/)[1];

      var c_kb_id = current.kb_knowledge_base.sys_id+'';

      var view;

      if (c_kb_id == '55ae9a6a0fec92004a98e64be1050e54') {

              view = '&sysparm_view=ITOC';

              gs.setRedirect(url+view);

      }

}

If a display BR is not the way to go how should I proceed? I would prefer not to use an onLoad client script since the form flashes and then it redirects.

Thanks!

1 ACCEPTED SOLUTION

tim_harris
Giga Expert

I found that the Display Business Rule was a dead end. It suffers from the same issue as changing views in the ui (requires update or insert action). I did however, come across another solution that is new in Geneva. As far as I can tell it was not mentioned anywhere in the release notes. View Rules now has an Advanced Script field!!



Here is a View Rule that works like a charm. :simple_smile:



Table: kb_knowledge


Advanced: checked


Script:


(function overrideView(view, is_list) {


      var url = gs.action.getGlideURI(); //get referring url


      var re = /sysparm_collectionID=(\w+)&?/; //set regex to get kb_knowledge_base ID from parameter


      var kb_id = re.exec(url)[1]; //run regex and set value


      if (kb_id == '55ae9a6a0fec92004a98e64be1050e54') // check to see if knowledge base is ITOC


              answer = 'itoc'; // Set the desired view


      else // If the knowledge base is not ITOC


              answer = null; // Do not set a view


})(view, is_list);



The sky is the limit! My example is checking against a parameter passed in the url, but I could also check against the current user object and other things of the like.


View solution in original post

9 REPLIES 9

Atul Kumar2
Giga Guru

Hi Tim,



Could you please try my suggestion may be it can help you.



1. get the class name(sys_class_name) if will match with kb_knowledge then only proceed with code.


2. write if else condition if it passes then it will execute your code else it will move ahead with no action resulting , it will work on every active link.



Hope this help.



Regards,


Atul Kumar


danpatino
Tera Expert

Hey Tim,



One thing I noticed before looking any further is the variable in your IF statement was declared on a line you commented out.   However, I would guess the resulting behavior would be it never redirecting (as opposed to always redirecting as you stated).



Also, just to rule this one out, can you confirm 'Global' isn't selected in the BR config?


Sorry, I updated the script above. I don't have it commented out in my BR. Also, the table is set to "kb_knowledge" not "global".


Maybe I'm missing the point of line 4 but could you just create a view rule checking for the particular knowledge base you have hardcoded?



If there is some additional logic you need (like matching category), you could always include that in your filter.



ServiceNow_OSI_Restaurant_Partners.jpg