- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-28-2015 09:02 AM
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!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2015 06:49 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-28-2015 10:26 AM
View rule will not work. My script above is just one use case. I want to be able to script based on anything (user attributes, etc)... Currently view rules have no advanced script option. The only work arounds I have found are a onLoad client script to perform a redirect, but this flashes a form and then reloads. Also, creating a ui macro, formatter, and then adding the formatter to the top of the form, but this seams like a lot of overhead.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-28-2015 10:33 AM
Agreed. Right now view rules are pretty limited. You can use javascript in the filters for some things but you're limited to one-liners unless you reference a script includes.
Just FYI, Geneva has the ability to create 'Advanced' filters where the return value of your script can be a view name. That probably doesn't help you right now though.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-28-2015 10:28 AM
I think I figured out what the issue is. I'm using the gs.setRedirect() command and then there is no update() or insert() action after. The redirect is set, but doesn't trigger until the next time I click something in ServiceNow. This results in a loop where every link goes to the redirect that I set earlier and the redirect is set again.
So now the question is: How can I trigger a redirect without update() or insert() from a business rule?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2015 05:35 AM
Could you perhaps fire an event, and then use a Script Action to actually perform the redirect?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2015 06:49 AM
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.