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

Hello Chris , thanks for the code , just adding all my other requirements please let me know if i need to change any 

find_real_file.png

From my point of view everything looks like it should work but the real test is running it and maybe adding some logging in there as you test it.

Nice, wouldn't have thought to use the URL.


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

gs.action.getGlideURI().getMap();

Always returns undefined when I try to log it from a view rule. If I try and run it from a background script I always get

*** Script: {runscript=Run script, quota_managed_transaction=on, sys_scope=0921915d0de28100c4b68319ed372f0f, record_for_rollback=on, script=var url = gs.action.getGlideURI().getMap();
gs.print(url);}

Any idea why?

 Edit: I had to ditch the .getMap() and then it works fine!  Logging from the view rule worked.

    var url = gs.action.getGlideURI();
    var isPortal = url.get('portal_id');
    var is16 = url.get('sys_id');

 

UI16:

url: incident.do?sys_id=ddc3bc7edbe32450f1736ce2ca96196e&sysparm_stack=&sysparm_view=
isPortal: null
is16: ddc3bc7edbe32450f1736ce2ca96196e

Portal:

url: api/now/sp/page?api=api&id=form&portal_id=8708a46b4f6117408b50a50f0310c7c6&request_uri=%2fup%3fsys_id%3d656480badbda6c108ef03533f3961944%26view%3dsp%26id%3dform%26table%3dincident&sys_id=656480badbda6c108ef03533f3961944&table=incident&time=1618862854899&view=sp
isPortal: 8708a46b4f6117408b50a50f0310c7c6
is16: 656480badbda6c108ef03533f3961944

Quite timely, I used this post to develop a View Rule last night and noticed the same behaviour.

I checked the API for getURIMap() and noticed there was no defined method for the getMap() function. It looks like you have already solved this but I came to the same solution as you.

var recSysId = gs.action.getGlideURI().get('sys_id');

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