- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 06-02-2020 08:30 PM
I occasionally see questions like "how can I allow a user to submit a form or catalog item, direct them to a new form or item, and pre-populate some of the previous data for them?" and there isn't an easy or obvious answer. It is, however, possible. Below I've outlined a workable solution.
It uses GlideAjax to put some client data on submit of one form or item, then uses a client script to check on load of another form or item to see if there is any matching session data and if found, sets values on the form or item.
This example uses two fields, one a text field named save_text and one a reference field named reference_field. As examples go it's very basic, but it's easily extensible.
GlideAjax Script Include
Name: setSession
Client Callable: True
var setSession = Class.create();
setSession.prototype = Object.extendsObject(AbstractAjaxProcessor, {
setSessionVar: function() {
var json = this.getParameter('sysparm_json');
gs.getSession().putClientData('json',json);
return "session set";
},
type: 'setSession'
});
Script to execute on submit to pull values from the form and set them as a session variable named "json".
Client Script: On Submit
Target: Your first form or item
function onSubmit() {
//Create an object
var strObj = {};
//Set the object to have properties that match our field names
strObj.save_text = g_form.getValue('save_text');
strObj.reference_field = g_form.getValue('reference_field');
var setSession = new GlideAjax('setSession');
setSession.addParam('sysparm_name', 'setSessionVar');
setSession.addParam('sysparm_json', JSON.stringify(strObj)); //Pass the object as a JSON string
setSession.getXML(
function (response) {
//var answer = response.responseXML.documentElement.getAttribute("answer");
//This doesn't need to "do" anything but, to test, you could pass back a value
return;
});
}
Then, on load, check and see if there is a session variable and if so, set the field values
Client Script: On Load
Target: Your second form or item
function onLoad() {
//Check is the session variable exists
if(g_user.getClientData('json')) {
//Take the string and convert it back to an object
var obj = JSON.parse(g_user.getClientData('json'));
//Set the values
g_form.setValue('save_text', obj.save_text);
g_form.setValue('reference_field', obj.reference_field);
}
}
If you want to use this on multiple items or forms you could add a property to the object you set that would be the name of the item or form then check if the client data exists and then check the name of the new property, etc.
Using this as a framework you could easily allow a user to start a request chain where some of the data is relevant across the chain and pre-populate that data for them within the same session. This also a useful method where a user might need to submit 30 requests for something where the only thing that changes between orders is the name of the person they are ordering for, etc.
It's not an every-day requirement, but when it arises it's always good to have a solution for it in your bag of tricks!
If you found this article helpful or useful, please be kind and click appropriately. If you found it really useful, you could always use the 3-dot menu to bookmark it for later!
Michael Jones - Proud member of the CloudPires team!
- 1,505 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I have done this in the past but do make sure to clear the data from session once it is consumed 🙂
gs.clearClientData(name);
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Great point! My particular use-case (preserve data from an original request to populate fields on subsequent requests) had the data persist while the session was active, but I could easily see it getting out of control if used too broadly! Always a good idea to clean-up after yourself!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Yes, making sure to clear the client data is very important since the data will be stored in memory. I have seen situations where excessive use of in-memory caching through putClientData() has run instances out of memory causing systemic performance issues. For a community post about memory topics in ServiceNow check out https://community.servicenow.com/community?id=community_question&sys_id=bc1bcfaddb5cdbc01dcaf3231f96...