logo

NJP

To create widget New - My Incident like My request widget on Service Portal

Import · Sep 09, 2020 · article

Hi Folks,

There has been the requirements of most of the clients to get a widget like 'My Request' on the Service Portal for other tables like Incidents.I am aware we can use 'My List' and update the instance options to display any of the table records but,as a fair practise and for my own development experience I did it as I have seen many users trying to achieve it.

Here is the code for the Widget 'My Incidents':

HTML

{{c.options.title}}

No incidents were raised

CSS:

.panel-heading { position: relative; > .fa-filter { position: absolute; top: 1rem; right: 1rem; }

}

.list-group-item > a { display: inline-block;

}

Client Side:

function ($location, $rootScope) { var c = this; this.filterText = ""; this.showFilter = false;

this.always_show=true;

this.onClick = function($event, item, url, action) { $event.stopPropagation(); $event.preventDefault(); if (url) $location.search(url); else { var evt = {}; evt.url = url; evt.table = c.data.table; evt.sys_id = item.sys_id; evt.record = item; evt.rectangle_id = c.data.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); } };

}

Server Side:

(function() {

if (!options.maximum_entries)

options.maximum_entries = 20;

var gr = new GlideRecordSecure('incident'); // does ACL checking for us

gr.addActiveQuery();

options.title = options.title || gr.getPlural();

data.display_field = 'short_description';

data.secondary_fields = ['number','sys_updated_on'];

data.filterMsg = gs.getMessage("Filter...");

gr.addEncodedQuery('caller_id=javascript:gs.getUserID()');

gr.orderByDesc('sys_created_on');

gr.query();

data.count = gr.getRowCount();

data.list = [];

var recordIdx = 0;

while (gr.next()) {

if (recordIdx == options.maximum_entries)

break;

var record = {};

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

var inc = new GlideRecord("incident");

inc.addQuery("sys_id", gr.getUniqueValue());

inc.query();

inc.next();

record.display_field = inc.getDisplayValue("short_description");

record.secondary_fields = [];

data.secondary_fields.forEach(function(f) {

record.secondary_fields.push(getField(gr, f));

});

record.url = {id: 'ticket', table: 'incident', sys_id: record.sys_id};

data.list.push(record);

recordIdx++;

}

function getField(gr, name) {

var f = {};

f.display_value = gr.getDisplayValue(name);

f.value = gr.getValue(name);

var ge = gr.getElement(name);

f.type = ge.getED().getInternalType()

f.label = ge.getLabel();

return f;

}

})()

Kindly share your thoughts on it if something looks inappropriate or not working.

Regards,

Munender Singh

Sr.Consultant Service-Now

Labels:

image

View original source

https://www.servicenow.com/community/developer-articles/to-create-widget-new-my-incident-like-my-request-widget-on/ta-p/2321237