- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2022 10:53 PM
I would like to automatically change Views between Portal and internal platform using View Rule. The condition is like below:
If
- Short description is "1"
AND
- Accessed from Service Portal
Then
Apply "u_1_portal" View
Else if
- Short description is "1"
AND
- Accessed from internal platform (NOT portal)
Then
Apply "u_1" View
I created a View Rule as per the advice in the following thread, but it doesn't work properly.
Could someone please modify the script to ​make it work?
https://community.servicenow.com/community?id=community_question&sys_id=63545a6bdbbe6700d6a102d5ca961937
(function overrideView(view, is_list) {
//get URL details
var url = gs.action.getGlideURI().getMap();
//Determine if in portal view
var isPortal = url.get('portal_id');
var gr = new GlideRecord('u_target_table');
if(gr.short_description == '1' && isPortal){
answer = 'u_1_portal';
}
if(gr.short_description == '1' && !isPortal ){
answer = 'u_1';
}
})(view, is_list);
Best Regards,
Aki
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2022 01:42 AM
Hi Aki,
Try the below code. Its working for me.
(function overrideView(view, is_list) {
//get URL details
var url = gs.action.getGlideURI().getMap();
//Determine if in portal view
var isPortal = url.get('id');
var sys_id = url.get('sys_id');
if(sys_id){
var gr = new GlideRecord('u_target_table');
if(gr.get(sys_id)){
if(gr.short_description == '1' && isPortal){
answer = 'u_1_portal';
}
if(gr.short_description == '1' && !isPortal ){
answer = 'u_1';
}
}
}else{
answer=null;
}
})(view, is_list);
Please mark it as helpful/correct If my answer is helpful in any way,
Best regards,
Thameem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2022 04:32 AM
Hi Thameem,
Here is the latest script. I added the info message at the beggining and noticed that the message didn't show up in the target table form (internal platform)...
*It worked for the portal.
(function overrideView(view, is_list) {
//Check if it's executed
gs.addInfoMessage("View Rule Executed");
//get URL details
var url = gs.action.getGlideURI().getMap();
//Determine if in portal view
var isPortal = url.get('id');
var sys_id = url.get('sys_id');
if(sys_id){
var gr = new GlideRecord('u_target_table');
if(gr.get(sys_id)){
if(gr.short_description == '1' && isPortal){
answer = 'u_1_portal';
}
if(gr.short_description == '1' && !isPortal ){
answer = 'u_1';
}
}
}else{
answer=null;
}
gs.addInfoMessage(answer);
})(view, is_list);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2022 05:57 PM
Hi Thameem,
After making a small change, the view rule worked perfectly!
Thank you so much.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2022 02:51 AM
Hi,
Please follow the steps below to achieve your requirement:
(function overrideView(view, is_list) {
// Add your code here
var isPortal = gs.action.getGlideURI().toString().indexOf('/sp');
if(isPortal > -1){
answer = 'Your View Name here'; //Replcae your View Name which you want in Portal
}else{
answer = 'Your View Name'; //For Native View
}
})(view, is_list);
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2022 03:05 AM
Hi shloke04,
Thank you for your script, but I need Short description == "1" condition as well.
Could you include it into your script, please?
If
- Short description is "1" //Field value of "u_target_table" record.
AND
- Accessed from Service Portal
Then
Apply "u_1_portal" View
Else if
- Short description is "1" //Field value of "u_target_table" record.
AND
- Accessed from internal platform (NOT portal)
Then
Apply "u_1" View
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2022 03:49 AM
Please use the modified script below:
(function overrideView(view, is_list) {
var url = gs.action.getGlideURI().getMap();
var sysId = url.get('sys_id');
var gr = new GlideRecord('incident'); //Pass your Table Name here
if (gr.get(sysId)) {
var shortDescription = gr.short_description;
var isPortal = gs.action.getGlideURI().toString().indexOf('/sp');
if (isPortal > -1 && shortDescription == 'Pass your Value here') {
answer = 'Your View Name here'; //Replcae your View Name which you want in Portal
} else {
answer = 'Your View Name'; //For Native View
}
}
// Add your code here
answer = null; // set the new view to answer
})(view, is_list);
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke