How to clear session data after submit catalog item?

zulhiyatiuwi
Tera Expert

Hello... I have UI action that save session data of number, short description and description of incident that i want to add to request variables

UI Action:

var session = gs.getSession();
session.putClientData('number', current.sys_id);
session.putClientData('short_description', current.short_description);
session.putClientData('description', current.description);

url="sp?id=sc_cat_item&sys_id=f959c1fa2f5a4010151e877cf699b6a9&sysparm_category=d258b953c611227a0146101fb1be7c31";

action.setRedirectURL(url);

 

And i have catalog client script in item:

function onLoad() {
    //Type appropriate comment here, and begin script below
    if (null != g_user.getClientData('number')) {
        var num = g_user.getClientData('number');
        g_form.setValue('incidentnum', g_user.getClientData('number'));
        var sd = g_user.getClientData('short_description');
        g_form.setValue('subjek', num);
        var desc = g_user.getClientData('description');
        g_form.setValue('deskripsi', desc);
    }
}

Its work, but how can i clear data session after i click the UI Action? I need to clear data after form request is load so, when i open the same form, the data will be empty. Please help. Thank you

find_real_file.png

1 ACCEPTED SOLUTION

Michael Jones -
Giga Sage

Ok, the only way I can seem to get this to work in a Catalog Item is to do it via a GlideAjax call (basically just shifting this from client-side to server side). I made a quick script include that looks like this: 

 

name: clientData
client callable: checked. 

var clientData = Class.create();
clientData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	type: 'clientData',
	clearData: function() {
		
		var session = gs.getSession();
		session.clearClientData('number');
                session.clearClientData('short_description');
                session.clearClientData('description');
		return 'clientData Cleared';
	},
	
});



And then in a client script call:

var ga = new GlideAjax('clientData');
ga.addParam('sysparm_name', 'clearData');
ga.addParam('sysparm_id', g_form.getValue('ref_server_name'));
ga.getXML(procResponse);

function procResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
//indicate the response in the console
jslog(answer);
}
I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

View solution in original post

11 REPLIES 11

Michael Jones -
Giga Sage

Ok, the only way I can seem to get this to work in a Catalog Item is to do it via a GlideAjax call (basically just shifting this from client-side to server side). I made a quick script include that looks like this: 

 

name: clientData
client callable: checked. 

var clientData = Class.create();
clientData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	type: 'clientData',
	clearData: function() {
		
		var session = gs.getSession();
		session.clearClientData('number');
                session.clearClientData('short_description');
                session.clearClientData('description');
		return 'clientData Cleared';
	},
	
});



And then in a client script call:

var ga = new GlideAjax('clientData');
ga.addParam('sysparm_name', 'clearData');
ga.addParam('sysparm_id', g_form.getValue('ref_server_name'));
ga.getXML(procResponse);

function procResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
//indicate the response in the console
jslog(answer);
}
I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Thank you. I will try it first 😄

Hello @Michael Jones , I have been trying the script. its work, but it still show error message "There is javascript error on your browser console" when i click ui action. So, I comment the script jslog(answer);. That is okay?

function onLoad() {
    //Type appropriate comment here, and begin script below
    var num = g_user.getClientData('number');
    var sd = g_user.getClientData('short_description');
    var desc = g_user.getClientData('description');
    if (null != num) {
        g_form.setValue('incidentnum', num);
        g_form.setValue('subjek', sd);
        g_form.setValue('deskripsi', desc);
    }

    var ga = new GlideAjax('clientData');
    ga.addParam('sysparm_name', 'clearData');
    ga.addParam('sysparm_id', g_form.getValue('ref_server_name'));
    ga.getXML(procResponse);
}

function procResponse(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    //indicate the response in the console
//     jslog(answer);
}

@zulhiyatiuwi 

Yes, if that line is giving you an error, it's fine to comment it out! I just used it to show that some response was sent. If it works, great!

If my answer was correct, please be kind and click appropriately! 

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Server-side cannot read data set client-side and vice versa. 

I ran into the same thing when trying to allow users to see inactive users on one catalog item that needed to interact with a query business rule.  I made a similar script include that just sets the [server-side] data and value of whatever I pass or clears it:

	setSessionToken: function(key, value){
		key = !key ? this.getParameter('sysparm_key') : key; //Key should be unique to the platform!
		value = !value ? this.getParameter('sysparm_value') : value;
		gs.getSession().putClientData(key, value);
	},
	/* Counterpart to setSessionToken.  Simply clears a token from a client. */
	clearSessionToken: function(key){
		key = !key ? this.getParameter('sysparm_key') : key;
		gs.getSession().clearClientData(key);
	},

Then my catalog item sets a token onLoad and removes it onSubmit.  So in the OP's case, the UI action can simply set the client data using gs.getSession().putClientData(key, value); and then use an onLoad client script to get the data, set the variables, and then remove it with a simple AJAX call (no callback needed):

function onLoad() {
	/* g_user.setClientData doesn't work when trying to read server side so a simply ajax is used to set it. Don't need to wait or do anything else. */
//Set values passed from the UI action with g_user.getClientData(datas)
//Then make an ajax call to myInclude to clear the data the UI action set.
	var ajax = new GlideAjax('myInclude');
	ajax.addParam('sysparm_name', 'clearSessionToken');
	ajax.addParam('sysparm_key', 'allowInactiveRead'); //Token to clear.
	ajax.getXML();
}

I think service portal handles this differently though.  No promises it'll work there (would just need a widget and just do it from the server script).

Also, make sure you use unique values for your data's key or you are clearing them!  They will stick with the user until they log out, which could cause problems on other forms using the same key name (which I found out I was doing today! 😯)