ServicePortal-Display the delegate approval on approvals widget

mohan91
Mega Expert

The approvals pending for the delegates are not displaying under approvals widget in service portal. I have tried to use the code from   getmyapprovals BR but am unable to achieve it.

Any help is appreciated.

5 REPLIES 5

Justin Abbott
Giga Guru

Can you post the code you attempted to use?


mohan91
Mega Expert

Hi   juabott,



please refer the below server script and commented lines related to the delegate approvals,



if (input && input.op) {


  var app = new GlideRecord("sysapproval_approver");


  if (app.get(input.target)) {


      app.state = input.op;


      app.update();


  }


}


/*function getMyApprovals() {


  var u = gs.getUserID();


  var answer = new Array();


  var i = 0;


  answer[i++] = new String(u);


  var g = new GlideRecord("sys_user_delegate");


  g.addQuery("delegate", u);


  g.addQuery("approvals", "true");


  g.addQuery("starts", "<=", gs.daysAgo(0));


  g.addQuery("ends", ">=", gs.daysAgo(0));


  g.query();


  while( g.next())


  gs.log("Hi");


  answer[i++] = new String(g.user);



  answer.push();


}*/




data.ViewApprovalPageMsg = gs.getMessage("View approval page");


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




var gr = new GlideRecord('sysapproval_approver');


gr.setLimit(3);


var qc1 = gr.addQuery("state", "requested");


if (input)


  qc1.addOrCondition("sys_id", "IN", input.ids);




gr.addQuery("approver", gs.getUserID());


gr.orderByDesc("sys_created_on");


gr.query();




var approvals = [];


var ids = [];


while (gr.next()) {


  var task = getRecordBeingApproved(gr);


  if (!task.isValidRecord())


  continue;




  ids.push(gr.getUniqueValue());



  var t = {};


  t.number = task.getDisplayValue();


  t.short_description = task.short_description.toString();


  if (task.isValidField("opened_by") && !task.opened_by.nil())


    t.opened_by = task.opened_by.getDisplayValue();




  // requestor >> opener


  if (task.isValidField("requested_by") && !task.requested_by.nil())


    t.opened_by = task.requested_by.getDisplayValue();




  t.start_date = task.start_date.toString();


  t.end_date = task.end_date.toString();


  t.table = task.getLabel();


  if (task.getValue("price") > 0)


    t.price = task.getDisplayValue("price");




  if (task.getValue("recurring_price") > 0)


    t.recurring_price = task.getDisplayValue("recurring_price");




  t.recurring_frequency = task.getDisplayValue("recurring_frequency");




  var items = [];


  var idx = 0;


  var itemsGR = new GlideRecord("sc_req_item");


  itemsGR.addQuery("request", task.sys_id);


  itemsGR.query();


  if (itemsGR.getRowCount() > 1)


      t.short_description = itemsGR.getRowCount() + " requested items";




  while (itemsGR.next()) {


      var item = {};


      item.short_description = itemsGR.short_description.toString();


      if (itemsGR.getValue("price") > 0)


          item.price = itemsGR.getDisplayValue("price");


      if (itemsGR.getValue("recurring_price") > 0) {


          item.recurring_price = itemsGR.getDisplayValue("recurring_price");


          item.recurring_frequency = itemsGR.getDisplayValue("recurring_frequency");


      }


      if (itemsGR.getRowCount() == 1) {


          item.variables = $sp.getRecordVariablesArray(itemsGR);


          t.short_description = itemsGR.short_description.toString();


      }




      items[idx] = item;


      idx++;


  }




  var j = {};


  j.sys_id = gr.getUniqueValue();


  j.table = gr.getRecordClassName();


  j.task = t;


  if (task)


      j.variables = $sp.getRecordVariablesArray(task);




  j.items = items;


  j.state = gr.getValue("state");


  j.stateLabel = gr.state.getDisplayValue();


  approvals.push(j);


}




data.ids = ids;


data.approvals = approvals;




//var link = {};


//link.title = gs.getMessage('View all approvals');


//link.type = 'link';


//link.href = '?id=approvals';


//link.items = [];


data.href ='?id=approvals';




function getRecordBeingApproved(gr) {


  if (!gr.sysapproval.nil())


      return gr.sysapproval.getRefRecord();




  return gr.document_id.getRefRecord();


}


Mohan,



I was able to modify the script and get it to work in my instance.



The query for the sysapproval_approver records was not referencing the delegates at all, so I added an or condition which calls the getMyApprovals function to return the delegators. I modified the getMyApprovals function a bit, as well.



Here's the code...



if (input && input.op) {


  var app = new GlideRecord("sysapproval_approver");


  if (app.get(input.target)) {


  app.state = input.op;


  app.update();


  }


}



function getMyApprovals() {


  var u = gs.getUserID();


  var answer = [];


  var i = 0;


  answer.push(u);


  var g = new GlideRecord("sys_user_delegate");


  g.addQuery("delegate", u);


  g.addQuery("approvals", "true");


  g.addQuery("starts", "<=", gs.daysAgo(0));


  g.addQuery("ends", ">=", gs.daysAgo(0));


  g.query();


  while( g.next())


  gs.log("Hi");


  answer.push(g.user.toString());



  return answer.toString();


}



data.ViewApprovalPageMsg = gs.getMessage("View approval page");


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



var gr = new GlideRecord('sysapproval_approver');


gr.setLimit(3);


var qc1 = gr.addQuery("state", "requested");


if (input)


  qc1.addOrCondition("sys_id", "IN", input.ids);



var qc2 = gr.addQuery("approver", gs.getUserID());


  qc2.addOrCondition("approver", "IN", getMyApprovals());


gr.orderByDesc("sys_created_on");


gr.query();



var approvals = [];


var ids = [];


while (gr.next()) {


  var task = getRecordBeingApproved(gr);


  if (!task.isValidRecord())


  continue;



  ids.push(gr.getUniqueValue());



  var t = {};


  t.number = task.getDisplayValue();


  t.short_description = task.short_description.toString();


  if (task.isValidField("opened_by") && !task.opened_by.nil())


  t.opened_by = task.opened_by.getDisplayValue();



  // requestor >> opener


  if (task.isValidField("requested_by") && !task.requested_by.nil())


  t.opened_by = task.requested_by.getDisplayValue();



  t.start_date = task.start_date.toString();


  t.end_date = task.end_date.toString();


  t.table = task.getLabel();


  if (task.getValue("price") > 0)


  t.price = task.getDisplayValue("price");



  if (task.getValue("recurring_price") > 0)


  t.recurring_price = task.getDisplayValue("recurring_price");



  t.recurring_frequency = task.getDisplayValue("recurring_frequency");



  var items = [];


  var idx = 0;


  var itemsGR = new GlideRecord("sc_req_item");


  itemsGR.addQuery("request", task.sys_id);


  itemsGR.query();


  if (itemsGR.getRowCount() > 1)


  t.short_description = itemsGR.getRowCount() + " requested items";



  while (itemsGR.next()) {


  var item = {};


  item.short_description = itemsGR.short_description.toString();


  if (itemsGR.getValue("price") > 0)


  item.price = itemsGR.getDisplayValue("price");


  if (itemsGR.getValue("recurring_price") > 0) {


  item.recurring_price = itemsGR.getDisplayValue("recurring_price");


  item.recurring_frequency = itemsGR.getDisplayValue("recurring_frequency");


  }


  if (itemsGR.getRowCount() == 1) {


  item.variables = $sp.getRecordVariablesArray(itemsGR);


  t.short_description = itemsGR.short_description.toString();


  }



  items[idx] = item;


  idx++;


  }



  var j = {};


  j.sys_id = gr.getUniqueValue();


  j.table = gr.getRecordClassName();


  j.task = t;


  if (task)


  j.variables = $sp.getRecordVariablesArray(task);



  j.items = items;


  j.state = gr.getValue("state");


  j.stateLabel = gr.state.getDisplayValue();


  approvals.push(j);


  }



  data.ids = ids;


  data.approvals = approvals;



  //var link = {};


  //link.title = gs.getMessage('View all approvals');


  //link.type = 'link';


  //link.href = '?id=approvals';


  //link.items = [];


  data.href ='?id=approvals';



  function getRecordBeingApproved(gr) {


  if (!gr.sysapproval.nil())


  return gr.sysapproval.getRefRecord();



  return gr.document_id.getRefRecord();


  }


Justin,

Is your modified code replacing the entire code in Server Script or just specific parts??

 

Thanks,

Brandon