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

RudhraKAM
Tera Guru

Thanks Chris for the reply , i just made some changes , and added logs but it seems not to be triggering , instead of answer i replaced it with view and its not working and i also checked by using answer too but not working 

 

Chris D
Kilo Sage
Kilo Sage

Hi, I know this is an old thread, but I want to provide an updated answer based on a ServiceNow case we had... like the answer suggested here (and other threads), we too were using 

var url = gs.action.getGlideURI().getMap();
var portal = url.get('portal_id');

to identify the Portal to apply a specific (non-sp) view... but we found that when you used a UI Action (Save, or a custom one), the Map returned is different, seemingly because it's not reloading the entire page - and most importantly, it doesn't contain the "portal_id" parameter, so it doesn't run the view rule and actually changes the view for the end user.

 

The solution provided to us is instead to use:

var url = gs.action.getGlideURI().toString();
var portal = ~url.indexOf( 'api/now/sp/' );

And I've confirmed this fixes that issue - it applies the view on both page load and after a UI Action is used.

On the downside - though it doesn't affect us - this just identifies IF it's a Portal, so if your view rule needed to run only for ONE specific Portal, then this wouldn't work (and I'm not sure the solution there...).