View Rule script to change Views between Portal and internal platform.

Aki17
Kilo Guru

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

1 ACCEPTED SOLUTION

Thameem Ansari
Giga Guru
Giga Guru

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

View solution in original post

16 REPLIES 16

Hi shloke04,

Thank you for modifying your script, but it doesn't work for both Portal and Native View...

Could you please revise the script and find out where the problem is?

Can you share your script version and also steps with screenshot on how are you testing it when you say it is not working for Portal?

Without screenshot it is difficult to assist you.

Regards,

Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke