We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

View rules for Form Widget

Ace009
Tera Contributor

Where's the logic for how the form widget utilizes View Rules?

 

Im trying to modify the OOTB approval page. Id like the related ticket's info be displayed via Form widget instead of the OOTB widget. Im able to display the related ticket by replacing the sys_id and table after line 69 (var = rec = $sp.getRecord() )  however the view is still the default view. 

3 REPLIES 3

lauri457
Tera Sage

You can pass view as an option or with a url parm view or v. Using the name "sp" for the Service portal view for example

data.view = options.view || $sp.getParameter("view") || $sp.getParameter("v"); // no default

 

Ace009
Tera Contributor

Hi Lauri,

So I dont want to pass in the view statically. I want the Form widget to use the View Rules associated with the record. 

 

The issue is Im using both the OOTB Form widget and the Approval Info widget on the Approval page. OOTB the Approval Page's URL is catered to the Approval Record. The Form Widget was parsing that and taking the Table + Sys ID which obviously doesnt exist. So I tried to modify the Form widget to simply switch the table and sys_id somewhere in line 70 with the variable "rec", which is a reference to the Task record associated with the approval. This worked and it showed the fields for the associated task but the View is the default view.

 

So instead, Im now passing another parameter in the URL (the approval sys_id) , modified the approval widget to use that sys_id instead, and now the Form widget works as well AND enforces the View Rules.

 

With that said and because Im curious lol, where in the Form widget's code does it return the View to rules via View Rules or is it one of those things that's under the hood via ServiceNow magic?

View rules are only for classic ui. There is no portal implementation, the logic probably being that the portal is a self service portal for ess users not an alternative ui.

You'd have to implement it yourself 

function getView(formRecordGr) {
	var viewsListGr = new GlideRecord("sysrule_view"), view = ""
	viewsListGr.orderByDesc("order")
	viewsListGr.addQuery("table", formRecordGr.getTableName())
	viewsListGr.query()

	while (viewsListGr.next()) {
		if (viewsListGr.getValue("advanced") == "1") {
			var evaluator = new GlideScopedEvaluator();
			evaluator.putVariable("view")
			evaluator.putVariable("is_list", false)
			evaluator.putVariable("answer")
			evaluator.evaluateScript(viewsListGr, 'script')
			view = evaluator.getVariable("answer")
		} else {
			if (GlideFilter.checkRecord(formRecordGr, viewsListGr.getValue("condition"), viewsListGr.getValue("match_conditions") === "ALL")) {
				view = viewsListGr.getValue("view")
			}
		}
		if (view) {
			return view
		}
	}
	return "sp"
}

var r = new GlideRecord("incident")
r.query()
while (r.next()) {
	gs.info(getView(r))
}