The CreatorCon Call for Content is officially open! Get started here.

Header Menu Issue - Scripted List

Carl Fransen1
Kilo Sage

I’m using the /hrportal in my implementation, which runs under it’s own scope.  I have created a new header menu in my portal called ‘Approvals’ – this is copied directly from the OOTB (Out of the box) Approvals for the /sp portal. 

Below shows the config of this:

This is a ‘Scripted List’ – details are below:

And the ‘Server Script’ code is below:

// CJF 14-2-18 Added this to allow Approval to be carried out from the HR Portal itself.
// only show 30 in header menu dropdown
var max = 30;

var t = data;
t.items = [];
t.count = 0;

//CJF 14-2-18 added global to the front to allow this to be called from the HR SP Scope.
var u = global.getMyApprovals();

// use record watchers to tell header when to update dropdown counts
t.record_watchers = [];
t.record_watchers.push({'table':'sysapproval_approver','filter':'approverIN' + u.toString() + '^state=requested'});

var z = new GlideRecord('sysapproval_approver');
z.addQuery("approver", u);
z.addQuery("state", "requested");
z.orderByDesc('sys_updated_on');
z.setLimit(max);
z.query();

var link = {};
link.title = gs.getMessage('View all approvals');
link.type = 'link';
link.href = '?id=approvals';
link.items = [];
t.items.push(link);

while (z.next()) {
  var a = {};
  var rec = getRecordBeingApproved(z);
	if (!rec.isValidRecord())  // nothing being approved - hmmm
		continue;
	
  a.short_description = rec.short_description + "";
  if (rec.getRecordClassName() == "sc_request") {
    var items = new GlideRecord("sc_req_item");
    items.addQuery("request", rec.getUniqueValue());
    items.query();
    if (items.getRowCount() > 1)
      a.short_description = items.getRowCount() + " requested items";
    else if (items.getRowCount() == 1) {
      items.next();
      a.short_description = items.getValue("short_description");
    }
  }
  $sp.getRecordValues(a, z, 'sys_id,sys_updated_on');
  a.number = rec.getDisplayValue();
  a.__table = z.getRecordClassName();
  a.type = 'approval';
  t.items.push(a);
  t.count++;
}

function getRecordBeingApproved(gr) {
  if (!gr.sysapproval.nil())
    return gr.sysapproval.getRefRecord();

  return gr.document_id.getRefRecord();
}

 

The only change I had to make (or it would throw an error) was to update line 10 to include the ‘global’ in front of the ‘getMyApprovals’.  Error was:

So fixed the above error – but I now receive these errors when opening the page or using this menu and I can’t get past it:

Line Number 48 is the below – it can be seen in context in the code snip above.

  $sp.getRecordValues(a, z, 'sys_id,sys_updated_on');

It would be great if someone could explain why $sp is not available in a scoped app and how I can work-around this, or fix this, so that I can use this?

 A previous article also asked this question, but unfortunately no answer was provided there either…

Would surely appreciate any assistance with this.

Cheers

Carl.

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

Can you try below

 

// only show 30 in header menu dropdown
var max = 30;

var t = data;
t.items = [];
t.count = 0;

var u = global.getMyApprovals();

// use record watchers to tell header when to update dropdown counts
t.record_watchers = [];
t.record_watchers.push({'table':'sysapproval_approver','filter':'approverIN' + u.toString() + '^state=requested'});

var z = new GlideRecord('sysapproval_approver');
z.addQuery("approver", u);
z.addQuery("state", "requested");
z.orderByDesc('sys_updated_on');
z.setLimit(max);
z.query();

// Use GlideAggregate for total count
var ga = new GlideAggregate('sysapproval_approver');
ga.addQuery("approver", u);
ga.addQuery("state", "requested");
ga.addAggregate('count', null);
ga.query();
if (ga.next()) {
  t.count = ga.getAggregate('count', null);
}

var link = {};
link.title = gs.getMessage('View all approvals');
link.type = 'link';
link.href = '?id=approvals';
link.items = [];
t.items.push(link);

while (z.next()) {
  var a = {};
  var rec = getRecordBeingApproved(z);
	if (!rec.isValidRecord())  // nothing being approved - hmmm
		continue;
	
  a.short_description = rec.short_description + "";
  if (rec.getRecordClassName() == "sc_request") {
    var items = new GlideRecord("sc_req_item");
    items.addQuery("request", rec.getUniqueValue());
    items.query();
    if (items.getRowCount() > 1)
      a.short_description = items.getRowCount() + " requested items";
    else if (items.getRowCount() == 1) {
      items.next();
      a.short_description = items.getValue("short_description");
    }
  }
  $sp.getRecordValues(a, z, 'sys_id,sys_updated_on');
  a.number = rec.getDisplayValue();
  a.__table = z.getRecordClassName();
  a.type = 'approval';
  t.items.push(a);
  //t.count++;
}

function getRecordBeingApproved(gr) {
  if (!gr.sysapproval.nil())
    return gr.sysapproval.getRefRecord();

  return gr.document_id.getRefRecord();
}

View solution in original post

5 REPLIES 5

Yes, Only change is scope. Code is same as OOTB.