Preserve Data after form submission: pass data between forms - using GlideAjax and g_form.getClientData()
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!
https://www.servicenow.com/community/developer-articles/preserve-data-after-form-submission-pass-data-between-forms/ta-p/2321872