Service Portal - how to save session info in storage (like using sessionStorage.setItem and getItem)

Renee-17
Tera Guru

I'm new to ServiceNow and Angular.  

 

I need to be able to store some session values into a variable so that upon reload of the page those values are available. 

 

Basically, I have a table where I am using dir-paginate.  The customer has the option to select the count for ItemsPerPage.  The list displayed in the table has a checkbox, then when checked, updates the specific record and reloads the page.  

 

The window.location.reload() works fine with the exception that I need to go back to the current paginated page, with the correct pagesize they selected earlier.  So I need to store these values somewhere so that upon reload the itemsPerPage and current-page information is available.

 

I have a similar scenario that works where I pass the data between Service Portal Pages using $sp.getParameter and $location.search and it works, but in this scenario I get to pass the values between pages.  How do I perserve these values on window.location.reload();

 

If you have any idea how to do this, besure to tell me what is in the template vs client script vs server script.

 

Thanks!

1 ACCEPTED SOLUTION

Rajdeep Ganguly
Mega Guru


In ServiceNow, you can use the GlideSession API to store session values. However, AngularJS does not have direct access to the server-side GlideSession API. You can use a GlideAjax call to interact with server-side scripts from the client-side AngularJS code.

Here's a general approach to solve your problem:

1. **Client Script (AngularJS):**
- Create a function to call a server-side script via GlideAjax.
- Pass the current page number and items per page as parameters to the server-side script.

2. **Server Script:**
- Create a Script Include that will be called by the GlideAjax from the client script.
- In this Script Include, use the GlideSession API to store the passed parameters as session values.

3. **Client Script (AngularJS):**
- After the page reloads, call another GlideAjax to retrieve the stored session values.
- Use these values to set the current page number and items per page.

Here's a sample code:

**Client Script (AngularJS):**

javascript
// Function to store session values
$scope.storeSessionValues = function(currentPage, itemsPerPage) {
var ga = new GlideAjax('SessionValues');
ga.addParam('sysparm_name', 'storeValues');
ga.addParam('sysparm_currentPage', currentPage);
ga.addParam('sysparm_itemsPerPage', itemsPerPage);
ga.getXML();
}

// Function to retrieve session values
$scope.getSessionValues = function() {
var ga = new GlideAjax('SessionValues');
ga.addParam('sysparm_name', 'getValues');
ga.getXMLAnswer(function(answer) {
var result = JSON.parse(answer);
$scope.currentPage = result.currentPage;
$scope.itemsPerPage = result.itemsPerPage;
});
}


**Server Script (Script Include):**

javascript
var SessionValues = Class.create();
SessionValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
storeValues: function() {
var currentPage = this.getParameter('sysparm_currentPage');
var itemsPerPage = this.getParameter('sysparm_itemsPerPage');
gs.getSession().putClientData('currentPage', currentPage);
gs.getSession().putClientData('itemsPerPage', itemsPerPage);
},

getValues: function() {
var currentPage = gs.getSession().getClientData('currentPage');
var itemsPerPage = gs.getSession().getClientData('itemsPerPage');
var result = {
currentPage: currentPage,
itemsPerPage: itemsPerPage
};
return JSON.stringify(result);
},

type: 'SessionValues'
});


Remember to call $scope.storeSessionValues() before reloading the page and $scope.getSessionValues() after the page reloads.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER

View solution in original post

3 REPLIES 3

ahefaz1
Mega Sage

@Renee-17 ,

 

I think you should be able to use the servicenow session API.

Please check this documentation - https://developer.servicenow.com/dev.do#!/reference/api/utah/server/c_GlideSessionScopedAPI

 

Please accept solution OR mark helpful.

 

Thanks,

Rajdeep Ganguly
Mega Guru


In ServiceNow, you can use the GlideSession API to store session values. However, AngularJS does not have direct access to the server-side GlideSession API. You can use a GlideAjax call to interact with server-side scripts from the client-side AngularJS code.

Here's a general approach to solve your problem:

1. **Client Script (AngularJS):**
- Create a function to call a server-side script via GlideAjax.
- Pass the current page number and items per page as parameters to the server-side script.

2. **Server Script:**
- Create a Script Include that will be called by the GlideAjax from the client script.
- In this Script Include, use the GlideSession API to store the passed parameters as session values.

3. **Client Script (AngularJS):**
- After the page reloads, call another GlideAjax to retrieve the stored session values.
- Use these values to set the current page number and items per page.

Here's a sample code:

**Client Script (AngularJS):**

javascript
// Function to store session values
$scope.storeSessionValues = function(currentPage, itemsPerPage) {
var ga = new GlideAjax('SessionValues');
ga.addParam('sysparm_name', 'storeValues');
ga.addParam('sysparm_currentPage', currentPage);
ga.addParam('sysparm_itemsPerPage', itemsPerPage);
ga.getXML();
}

// Function to retrieve session values
$scope.getSessionValues = function() {
var ga = new GlideAjax('SessionValues');
ga.addParam('sysparm_name', 'getValues');
ga.getXMLAnswer(function(answer) {
var result = JSON.parse(answer);
$scope.currentPage = result.currentPage;
$scope.itemsPerPage = result.itemsPerPage;
});
}


**Server Script (Script Include):**

javascript
var SessionValues = Class.create();
SessionValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
storeValues: function() {
var currentPage = this.getParameter('sysparm_currentPage');
var itemsPerPage = this.getParameter('sysparm_itemsPerPage');
gs.getSession().putClientData('currentPage', currentPage);
gs.getSession().putClientData('itemsPerPage', itemsPerPage);
},

getValues: function() {
var currentPage = gs.getSession().getClientData('currentPage');
var itemsPerPage = gs.getSession().getClientData('itemsPerPage');
var result = {
currentPage: currentPage,
itemsPerPage: itemsPerPage
};
return JSON.stringify(result);
},

type: 'SessionValues'
});


Remember to call $scope.storeSessionValues() before reloading the page and $scope.getSessionValues() after the page reloads.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER

Renee-17
Tera Guru

Thanks! You're detailed response was very helpful.