Javascript in URL from Arguments not working

reginabautista
Kilo Sage

Hi guys

I have a menu module with Link Type = URL from Arguments. I have created a function in Script Include (client callable) to build the condition/url. However for some reason, javascript is coming back as NULL.I have tested my code in Background scripts and I am pretty sure that that it is building the the correct url for the condition.

I'm not sure if the function is being called at all.. Could you let me know what I am missing. I am at my wits end

find_real_file.png

Script:

var getMyReqApprovals = Class.create();

getMyReqApprovals.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  getMyReqApprovals: function(){

  var u = gs.getUserID();

  var answer;

  var nq;

  var rowCount;

  var ctr =0;

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

  rowCount = g.getRowCount();

  while( g.next()){

  ctr = ctr + 1;

  gs.log('ctr=' + ctr + ' of ' + rowCount);

  if(ctr==rowCount){

  nq='';

  }else{

  nq='^NQ';

  }

  answer+= 'approver='+ g.user + '^u_delegate_type=' + g.u_delegate_type + nq;

  }

  gs.log('myapprovers=' + answer);

  return answer;

  },

  type: 'getMyReqApprovals'

});

4 REPLIES 4

BALAJI40
Mega Sage

I guess you are forget to call the function name, try with this


sysapproval_approver_list.do?sysparm_query =javascript: new getMyReqApprovals ().getMyReqApprovals()


Chuck Tomasi
Tera Patron

Hi Regina,



You clicked "client callable" in the script include (the clue is that your script include extends the abstract ajax processor.)



Use this for your entire script include (and uncheck client callable.) Note, the name of the script include MUST be 'getMyReqApprovals'. I'll assume you are already constructing a valid encoded query string (your answer)



function getMyReqApprovals {


  var u = gs.getUserID();


  var answer;


  var nq;


  var rowCount;


  var ctr =0;


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


  rowCount = g.getRowCount();


  while( g.next()) {


      ctr = ctr + 1;



      gs.log('ctr=' + ctr + ' of ' + rowCount);


      if (ctr == rowCount) {


          nq='';


      } else {


          nq='^NQ';


      }



  answer+= 'approver='+ g.user + '^u_delegate_type=' + g.u_delegate_type + nq;


  return answer;


  }


Thanks guys for the reply.



After several hours of frustration, this has now been resolved. The solution is not pretty but does the job (displays several sys_ids in the List view condition)



The issue was in this context (URL from Arguments), my script wasn't triggered because I didn't equate it to any fields.



Original URL:


sysapproval_approver_list.do?sysparm_query=javascript:getMySCReqApprovals()



Now when I have equate it to the approver field, the code is being called successfully. I thought I would get away with the above URL as it can be as dynamic as I want it to be. Apparently it's not the case.



New URL:


sysapproval_approver_list.do?sysparm_query=approver=javascript:getMySCReqApprovals()



That was the first issue. As the new URL will not meet my requirements, this is what have worked:



This is a modification to the OOTB getmyApprovals() Businesss Rule which handles delegates.



Chuck, for some reason the push method does not work so I had to keep the out of the box handling of arrays.




Business Rule:


function getMySCReqApprovalsSysID() {


  var u = gs.getUserID();


  var answer=[];


  var runArr = [];


  var nq;


  var rowCount;


  var ctr =0;


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


  rowCount = g.getRowCount();


  //gs.log('delegate count=' + rowCount);


  while( g.next()){


  answer.push(getAppRecSysIDs(g.user,g.u_delegate_type,g.user.getDisplayValue()));


  ctr++;


  gs.log('ctr ' + ctr + ' of ' + rowCount);


  //gs.log('result=' + getAppRecSysIDs(g.user,g.u_delegate_type,g.user.getDisplayValue()));


  }



  //return answer.concat();


  return answer.join();


}




function getAppRecSysIDs(approver,delegatetype, dispUser){


  var answer = new Array();


  var i = 0;


  var sysids = [];


  var app = new GlideRecord('sysapproval_approver');


  app.addQuery('approver', approver);


  app.addQuery('u_delegate_type',delegatetype);


  app.query();


  gs.log('rows=' + app.getRowCount() + ' dispUser=' + dispUser);


  while(app.next())


  answer[i++] = new String(app.sys_id);



  return answer;



}




My Approvals Menu:



find_real_file.png


Glad you got it sorted out Regina.