Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

I dont like the solution with the session.

 

Here is how I do it ... 

 

// Business Rule on Display

g_scratchpad.link = '';

// Get the instance name
var instanceName = gs.getProperty('instance_name');

// Check if necessary fields are set
var sysId = current.sys_id;
var user = current.assigned_to.sys_id;

// Construct the full URL in one go
g_scratchpad.link = 'https://' + instanceName + '.service-now.com/XXXX_WHERE_EVER_U_WANT_TO_GO_XXXXX&sys_id=' + sysId 
    + '&sys_param_type=link' 
    + '&sys_param_srn_user=' + user;

 

// UI Action
    var url = g_scratchpad.link; 
    getTopWindow().popupOpenFocus(url, "_self", null, null, "");

 

// on load client script

var parmLink = getParmVal('sys_param_type') || '';
g_scratchpad.prefillUser = getParmVal('sys_param_srn_user') || '';

if (parmLink === 'link' && g_scratchpad.prefillUser.toString() !== '') {
    g_form.setValue('assigned_to', g_scratchpad.prefillUser);
}

function getParmVal(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(top.location);
    return results == null ? "" : decodeURIComponent(results[1]).toString();
}

 

Quite Flexible and could be done in a generic way to that you could handle multiple use cases within one BR / UI Action / CS ... 

Hi,

 

May I know where did you get ref_server_name in this line?

ga.addParam('sysparm_id', g_form.getValue('ref_server_name'));

 

Thank you!