Use of Global 'getMyApprovals' Business Rule in HR Service Portal

Carl Fransen1
Tera Guru

Hi Team,

As part of the HR Portal setup I am adding the ability to manage your approval tasks, much like we can in the IT Portal.

I updated the main menu 'HR Header' to include a 3rd Menu Item called 'Approvals'.   I copied the script and setup from the IT Portal - the script entered is below and I have had to change line 10 to include the 'global' in front of the 'getMyApprovals'.

// 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();

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.doc

I also had to update the Business Rule 'getMyApprovals' and change the 'Accessible from' field from 'This application scope only' to 'All application scopes'.

Without these two changes I would receive the following errors:

1. When the BR scope was incorrect:Illegal access to package_private global script getMyApprovals: caller not in scope rhino.global

or

2. When the script didn't include the 'global':

Server JavaScript error "getMyApprovals" is not defined.

Line number 8

Script source code logged to browser console

Failing widget: 'Header Menu' (5ef595c1cb12020000f8d856634c9c6e) called from: 'HRI Header-CJF' (7bfc2751db2e0b007d1bfa0dbf9619f5)

My question is essentially wanting to confirm if this is the correct way to do this?   I was hoping that there would be a simpler way to do this as the HR Portal pages allow approvals to occur OOTB but don't provide a way to use the code from the IT Portal without these changes.   I'm keen to know if there are ways where it wouldn't impact OOTB records which will have longer term upgrade implications.

Any guidance is appreciated.

1 ACCEPTED SOLUTION

Carl Fransen1
Tera Guru

Hi All,

After logging an issue with HI, I have finally managed to get this working - essentially all I had to do was create an approvals menu in the /hrportal Main Menu with a 'Global' scope instead of the 'HR SP' scope - see below:

 

find_real_file.png

 

Works like a treat now.  Hope this helps someone

Cheers

Carl.

View solution in original post

4 REPLIES 4

Jon Barnes
Kilo Sage

I think what you did is fine. If you want to do it without updating the OOB getMyApprovals Business Rule, you could copy the script from there and create a new global script include where you put a GlobalUtils.getMyApprovals (or call it what you want) function for your scoped apps to use, but either way is acceptable in my view


Hi Jon,

I was doing more testing now we are close to implementing this into production and there is a new error appearing:

find_real_file.png

turns out that line 48 is the following:    $sp.getRecordValues(a, z, 'sys_id,sys_updated_on');

so the approvals work, but they always present the errors above when completing the approvals. 

I had to change the line: var u = global.getMyApprovals(); to include the 'global.' in front of the function, wondering if there is anything else I need to do to use the $sp from the HR Scope....?

Hoping you can help...

Cheers

Carl.

 

Carl Fransen1
Tera Guru

Hi All,

Turns out if I remove the 'Approvals' menu option form the 'HR Header' all the errors disappear -and the approvals work fine without any issues.  Except I can't see all my approvals in the menu, only inside the 'todo ' list.

There must be a way we can re-sue the 'Approvals' menu from the /sp portal - hoping someone is able to assist.

 

Cheers

Carl.

Carl Fransen1
Tera Guru

Hi All,

After logging an issue with HI, I have finally managed to get this working - essentially all I had to do was create an approvals menu in the /hrportal Main Menu with a 'Global' scope instead of the 'HR SP' scope - see below:

 

find_real_file.png

 

Works like a treat now.  Hope this helps someone

Cheers

Carl.