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.

ServicePortal redirect URL

Salzo
Giga Contributor

Hello,

 

I am trying to change the ootb simple list widget to redirect the user a webpage outside servicenow.

 

At the moment when I click the link I want to trigger the redirect instead of going to https://www.newurl.com the user is being sent to http://instance.service-now.com/www.newurl.com.

 

So far I have tried

 

gs.setRedirect(url);
location.href(url);
$location.href(url);

$location.path(url);

 

 

How do I do this?

8 REPLIES 8

As I see you already have the parameter :

function ($scope, $location, $rootScope, spUtil, $interpolate) {.......

 

In your script, you have:

this.onClick = function($event, item, url, action) {
$location.href(url)

}

If you check into the HTML part, you will see :

<div class="panel panel-{{::c.options.color}} b" ng-if="c.data.isValid && (c.options.always_show || c.data.filterText || c.data.list.length)">
<div class="panel-heading" ng-if="::!c.options.hide_header">
<h2 class="h4 panel-title">
<span ng-if="c.options.glyph">
<fa name="{{::c.options.glyph}}" />
</span>{{::c.options.title}}</h2>
<!-- <i class="fa fa-filter" ng-click="c.toggleFilter()" ng-class="{'disabled-filter': !c.showFilter}"></i> -->
<div ng-show="c.showFilter">
<input aria-label="${Filter}" ng-model="c.data.filterText" ng-model-options="{debounce: 300}" sn-focus="c.showFilter" placeholder="{{::data.filterMsg}}" ng-change="c.update()" class="form-control input-sm filter-box">
</div>
</div>
<ul class="list-group hide-x-overflow" style="max-height: {{::c.options.panel_body_height || 'none'}}; overflow-y: auto;">
<li ng-if="c.data.list.length > 0" ng-repeat="item in c.data.list track by item.sys_id" class="list-group-item">
<a ng-click="c.onClick($event, item, item.url, {})" href="javascript:void(0)" >
<span ng-repeat="action in c.data.actions" href="" ng-click="c.onClick($event, item, action.url, action)" ng-if="action.glyph"
class="list-action l-h-40 pull-right">
<fa name="{{action.glyph}}" ng-class="c.getActionColor(action)" />
</span>
<span ng-if="c.options.image_field" class="pull-left m-r"
ng-class="{'avatar': c.options.rounded_images, 'thumb-sm': c.options.rounded_images}">
<img ng-src="{{item.image_field}}" alt="..." class="img-sm" ng-class="{'img-circle': c.options.rounded_images}">
</span>
<div>

 

Please see this: ng-click="c.onClick($event, item, action.url, action)"

You will need to update the value of the variable action.url.

In server script, modify this part as you want (with record.url variable):

if (options.sp_page) {
var view = "sp";
if (options.view) {
var viewGR = new GlideRecord("sys_ui_view");
viewGR.get(options.view);
view = viewGR.getValue("name");
}
record.url = {id: options.sp_page, table: record.className, sys_id: record.sys_id, view: view};
} else if (options.url != '')
record.url = options.url;
else
record.url = null;

 

Hope this will guide you to the answer 🙂

Regards

Anaïs

 

Thanks for your reply!

 I did update record.url but all that happens is now it appends the url onto the instance domain. I need to know how to clear $location of all info and replace it with www.myurl.com.

If  you could give me some more clues I would be greatly appreciative 

 


if (name.indexOf("sys_") == -1)
record[name] = gr.getValue(name);
}
}


record.url = gr.getValue('u_url');
record.sys_id = gr.getValue('sys_id');


record.className = gr.getRecordClassName();

Hello,

 

Here is the solution I propose:

  1. Check your page structure and verify that your instance class is an "Instance of Simple List"
  2. Do not set any options for ticket page or URL (via Designer)

Then you can modify the code of your cloned Widget of Simple List:

Server:

if (options.sp_page) {
			var view = "sp";
			if (options.view) {
				var viewGR = new GlideRecord("sys_ui_view");
				viewGR.get(options.view);
				view = viewGR.getValue("name");
			}
			record.url = {id: options.sp_page, table: record.className, sys_id: record.sys_id, view: view};
		} else if (options.url != '' && options.url.indexOf('undefined') == -1){
			record.url = options.url;
		}
		else if (options.url.indexOf('undefined') != -1){
			record.url = "https://www.google.lu";
		}
		else{
			record.url = null;
		}

 

Client:

function ($scope, $location, $rootScope, spUtil, $interpolate, $window) {
	var c = this;

	this.data.filterText = "";
	this.showFilter = false;

	this.onClick = function($event, item, url, action) {
		$event.stopPropagation();
		$event.preventDefault();
		if (typeof url == "string") {
                        // Redirection to external URL   
			$window.location.href = url;
			/*Part of the code to comment
                        var urlExp = $interpolate(url);
			url = urlExp(item);
			$location.url(url);*/
		} else if (url && typeof url == "object")
			$location.search(url);
		else {
			var evt = {};
			evt.url = url;
			evt.table = item.className;
			evt.sys_id = item.sys_id;
			evt.record = item;
			evt.rectangle_id = c.options.sys_id;
			evt.action = action;
			// put out the selection with simple list "sl_" prefix
			$location.search('sl_sys_id', evt.sys_id);
			$location.search('sl_table', evt.table);
			$location.search('spa', 1); // spa means "I've got this"
			$rootScope.$broadcast('$sp.list.click', evt);
		}
	};

	if (c.options.table)
		spUtil.recordWatch($scope, c.options.table, c.options.filter);

	this.getMaxShownLabel = function(maxEntries, totalCount) {
		if (totalCount == c.data.maxCount)
			return "${First [0] of more than [1]}".replace('[0]', maxEntries).replace('[1]', totalCount);

		return "${First [0] of [1]}".replace('[0]', maxEntries).replace('[1]', totalCount);
	};

	this.seeAllPage = c.options.list_page_dv || 'list';
	this.targetPageID = (c.options.sp_page) ? "&target_page_id=" + c.options.sp_page : "";

	c.getActionColor = function(action) {
		return "text-" + action.color;
	};

	c.update = function update() {
			c.server.update();
	}

	c.toggleFilter = function() {
		c.showFilter = !c.showFilter;
	}

}

 

Don't forget to add the variable $window into the function parameters 😉 

Regards

Anaïs

 

 

Salzo,

Is this resolved now?

Regards