Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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 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);

Hi Thameem,

After making a small change, the view rule worked perfectly!

Thank you so much.

shloke04
Kilo Patron

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);

find_real_file.png

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

Regards,
Shloke

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

Regards,
Shloke

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

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

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

Regards,
Shloke