How to access session variable in a client callable script include

DhananjayM
Tera Guru

Hi All,

I am working on creating a context aware report where I am setting a reference field value using a look up field. This report is added to a page which has a list of all projects

Page contains a drop down field with list of projects and on selection of project, I reload the page and filter the contents of report based on selected project.

Report - Task status

Table - pm_proejct_task

Filter condition

Project = javascript:getSelectedProject()

To achieve this I am using session variable gs.getSession.putClientData('prj_id',<current prj value>). I can do this using standard GlideAjax script.

Now I would presume that if a value of session variable is set I can use that in a client callable script include getSelectedProject

My script include "getSelectedProject" looks like

function getSelectedProject() {

var array= new Array();

var prj_id = gs.getSession().getClientData('prj_id');

gs.log('selected project ' +prj_id );

var gr = new GlideRecord('pm_project_task');

gr.addQuery('parent',prj_id);

gr.addActiveQuery();

while(gr.next()) {

          array.push(gr.sys_id.toString());

}

    return array;

}

Every time I run the report the session variable 'prj_id ' always comes as undefined. I even tried setting this session variable manually using script background. But even this doesn't work.

Any thoughts?

Thanks,

Dhananjay

1 ACCEPTED SOLUTION

DhananjayM
Tera Guru

Finally I found a way out of this issue. I realized that session values are accessible only when we have a script action tied to session.established event. http://wiki.servicenow.com/index.php?title=Session_Client_Data#gsc.tab=0



So to overcome this I made a small change. On change of the drop down value instead of using session variable I used user preference and set that value in the client script (via Ajax)



function onDropDownChange(this) {


  var value = this.value;


  setUserPref('prj_id',value);


}



function setUserPref(name, value) {


  var ga = new GlideAjax('AjaxUtil');


  ga.addParam('sysparm_name','setUserPreference');


  ga.addParam('sysparm_var_name', name);


  ga.addParam('sysparm_var_value', value);


  ga.getXMLWait();


}



In AjaxUtil I created getter and setter method to manipulate user preference value



setUserPreference: function() {


strVarName = this.getParameter('sysparm_var_name');


strVarValue = this.getParameter('sysparm_var_value');


gs.getUser().setPreference(strVarName, strVarValue);


return strVarName + " has been set to " + strVarValue;


}




getUserPreference: function() {


strVarName = this.getParameter('sysparm_var_name');


return gs.getPreference(strVarName);


}



On server side script "getSelectedProject" the code looks almost the same except that we now get value using user preference



function getSelectedProject() {


  var array= new Array();


  var prj_id = gs.getPreference("prj_id"); //This will fetch value from user preference


  var gr = new GlideRecord('pm_project_task');


  gr.addQuery('parent',prj_id);


  gr.addActiveQuery();


  while(gr.next()) {


  array.push(gr.sys_id.toString());


  }


  return array;


}



Hope this helps!


View solution in original post

1 REPLY 1

DhananjayM
Tera Guru

Finally I found a way out of this issue. I realized that session values are accessible only when we have a script action tied to session.established event. http://wiki.servicenow.com/index.php?title=Session_Client_Data#gsc.tab=0



So to overcome this I made a small change. On change of the drop down value instead of using session variable I used user preference and set that value in the client script (via Ajax)



function onDropDownChange(this) {


  var value = this.value;


  setUserPref('prj_id',value);


}



function setUserPref(name, value) {


  var ga = new GlideAjax('AjaxUtil');


  ga.addParam('sysparm_name','setUserPreference');


  ga.addParam('sysparm_var_name', name);


  ga.addParam('sysparm_var_value', value);


  ga.getXMLWait();


}



In AjaxUtil I created getter and setter method to manipulate user preference value



setUserPreference: function() {


strVarName = this.getParameter('sysparm_var_name');


strVarValue = this.getParameter('sysparm_var_value');


gs.getUser().setPreference(strVarName, strVarValue);


return strVarName + " has been set to " + strVarValue;


}




getUserPreference: function() {


strVarName = this.getParameter('sysparm_var_name');


return gs.getPreference(strVarName);


}



On server side script "getSelectedProject" the code looks almost the same except that we now get value using user preference



function getSelectedProject() {


  var array= new Array();


  var prj_id = gs.getPreference("prj_id"); //This will fetch value from user preference


  var gr = new GlideRecord('pm_project_task');


  gr.addQuery('parent',prj_id);


  gr.addActiveQuery();


  while(gr.next()) {


  array.push(gr.sys_id.toString());


  }


  return array;


}



Hope this helps!