help with View rules.

RudhraKAM
Tera Guru

I have a requirement where  For End Users on certain conditions it should show one view , below are the conditions 

A. If the user opening an RITM has no itil roles, and the item is not being opened from a  portal url, use the ess view

 B. If the user opening an RITM has itil role, and the item is not being opened from a portal url, use the default view

C. If the user opening an RITM has itil role, and the item is being opened from a  portal url, and the requested item is from the Software Catalog, and the item is not closed incomplete, use the sp view.

 

can some one help me with this below View rule script and how to add the condition if the item is being opened from portal or not 

 

(function overrideView(view, is_list) {

//A. 
if (!gs.hasRole("itil") );
view =ess;

//B. 
if (gs.hasRole("itil"))
view="";

//C

if (gs.hasRole("itil") && current.item.sc_catalogs=="57bf1d3e13635b00602dbcaf3244b0a0" && current.state!="4");
view =sp;
answer = null; // set the new view to answer

})(view, is_list);

1 ACCEPTED SOLUTION

ChrisBurks
Mega Sage

This script should capture all three. One caveat though is that the script will only run for portal if using the "form" page and not the "ticket" page. The ticket page is built a little bit differently and doesn't kickoff to the back end the view rule. The form page does. Thus, you would have to view RITMs from form page.

(function overrideView(view, is_list) {
	
	answer = view;
	
	//get URL details
	var url = gs.action.getGlideURI().getMap();
	
	// parse url for information needed
	var recSysId = url.get('sys_id');
	var isPortal = url.get('portal_id');
	
	//Condition 1
	if( !gs.hasRole('itil') && !isPortal ){
		answer = 'ess';
	}
	
	// Condition 2
	if( gs.hasRole('itil') && !isPortal ){
		answer = view || 'default';
	}
	
	// Condition 3
	if( gs.hasRole('itil') && isPortal ){
		var ritm = new GlideRecord('sc_req_item');
		var state;
		var catType;
		if(ritm.get(recSysId)){
			state = ritm.getDisplayValue('state');
			catType = ritm.cat_item.sc_catalogs.getDisplayValue();
		}
		
		if(state != 'Closed Incomplete' && catType.indexOf('Software Catalog') > -1){
			answer = 'sp';
		}
	}
		
})(view, is_list);

 

Note: I have not tested this fully. Only against the different Portal pages and platform page to see if things logged out.

 

View solution in original post

11 REPLIES 11

The SN Nerd
Giga Sage
Giga Sage

View Rules can either be based on conditions, or based on roles.
They cannot be based on both, so your requirement for C cannot be met.

current is not available to advanced scripts in view rules, so C will not work.
You would have to write this as a conditional view rule, but would not be able to check for itil role.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

oh thanks for the reply,,

 Can I use multiple If conditions in the same View rule?

and how to include the current session in the condition? I came to know that we can use gs.getUrlOnStack but not sure how.

RudhraKAM
Tera Guru

Hello Paul , then how do we achieve the solution for c ? 

ChrisBurks
Mega Sage

This script should capture all three. One caveat though is that the script will only run for portal if using the "form" page and not the "ticket" page. The ticket page is built a little bit differently and doesn't kickoff to the back end the view rule. The form page does. Thus, you would have to view RITMs from form page.

(function overrideView(view, is_list) {
	
	answer = view;
	
	//get URL details
	var url = gs.action.getGlideURI().getMap();
	
	// parse url for information needed
	var recSysId = url.get('sys_id');
	var isPortal = url.get('portal_id');
	
	//Condition 1
	if( !gs.hasRole('itil') && !isPortal ){
		answer = 'ess';
	}
	
	// Condition 2
	if( gs.hasRole('itil') && !isPortal ){
		answer = view || 'default';
	}
	
	// Condition 3
	if( gs.hasRole('itil') && isPortal ){
		var ritm = new GlideRecord('sc_req_item');
		var state;
		var catType;
		if(ritm.get(recSysId)){
			state = ritm.getDisplayValue('state');
			catType = ritm.cat_item.sc_catalogs.getDisplayValue();
		}
		
		if(state != 'Closed Incomplete' && catType.indexOf('Software Catalog') > -1){
			answer = 'sp';
		}
	}
		
})(view, is_list);

 

Note: I have not tested this fully. Only against the different Portal pages and platform page to see if things logged out.