How to open newly created requested item record in a new tab from Ui action which is on case Table in agent workspace?

HARI KISHAN GVS
Mega Sage

Hi Team, 

as part of one of my requirement, i created a UI Action which is on case table. by clicking on the action new request and request item records will generate with some field values copied from case to request and request item and form will redirect to generated request item record.

it is working fine in instance side But in Agent workspace side records are generating but generated requested item is not opening in new tab.  'record not found' error is showing.

Please fix my Ui Action  Workspace Client script to open generated requested item record in a new tab  in agent workspace side.

-----------------------Script---------------------------------

createServiceRequest();

function createServiceRequest() {
try {
var reqSysid, ritmSysid;
var itemId = 'a2a030391b800d10f6f7ddf2cc4bcbae'; // Catalog Item sys_id

// create new cart
var cartId = gs.generateGUID();
var cartGr = new sn_sc.CartJS(cartId); // Glide Record
gs.info("Cart [" + cartId + "] has system id: " + cartGr.getCartID());
var cntct = current.contact.toString();
var req_by = current.opened_by.toString();
var request = {
'sysparm_id': itemId,
'sysparm_quantity': '1',
'variables': {
'short_description': 'Test Catalog Item', // here list of variables for this item
'requested_by': req_by,
'customer_contact': cntct
}
};
// add item to cart
var cartDetails = cartGr.addToCart(request); // JS object
for (var i in cartDetails) {
gs.info("Cart Details: " + i + "=" + cartDetails[i]);
}
// check what is in the cart
cartItems = cartGr.getCartItems(); // Glide Record
// checkout cart
var checkoutInfo = cartGr.checkoutCart(); // JS Object
// check checkout response
for (var k in checkoutInfo) {
gs.info("CheckoutInfo: " + k + "=" + checkoutInfo[k]);
}
// Update Catalog Request
var reqGr = new GlideRecord('sc_request');
reqGr.get(checkoutInfo.request_id);
reqGr.setValue('requested_for', current.contact);
reqGr.setValue('opened_by', current.opened_by);
reqGr.setValue('short_description', 'Part Return Request');
reqGr.setValue('assignment_group', current.assignment_group);
reqGr.setValue('assigned_to', current.assigned_to);
reqGr.setValue('parent', current.sys_id);

reqSysid = reqGr.sys_id;
reqGr.update();
// Update ordered items tasks (RITMs)
var itemGr = new GlideRecord('sc_req_item');
while (cartItems.next()) {
gs.info("Cart Item: " + cartItems.getValue('sys_id'));
itemGr.initialize();
itemGr.addQuery('request', checkoutInfo.request_id);
itemGr.query();
itemGr.setValue('requested_for', current.contact);
itemGr.setValue('opened_by', current.opened_by);
itemGr.setValue('assignment_group', current.assignment_group);
itemGr.setValue('assigned_to', current.assigned_to);
itemGr.setValue('parent', current.sys_id);
ritmSysid = itemGr.sys_id;
itemGr.updateMultiple();
}
action.setRedirectURL("sc_req_item?sys_id=" + ritmSysid + "&sysparm_view=default&sysparm_view_forced=true" + current.sys_id);
} catch (ex) {
gs.error("Exception: " + ex);
}
}

------------ Workspace Client Script----------------------

function onClick(g_form) {
g_aw.openRecord('sc_req_item', 'ritmSysid');
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

find_real_file.png

Thanks in advance,

Hari Kishan.

1 ACCEPTED SOLUTION

HARI KISHAN GVS
Mega Sage

Hi Hari,

Remove the action.setRedirectURL statement in the Script Block and remove script in the Workspace ClientScript block and Uncheck the Client Check box and add the following two lines in the script in the place of action.setRedirectURL.

action.openGlideRecord(itemGr);
action.setReturnURL(current);

 

Thank you!

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

workspace client script would require GlideAjax to create those things

Are you using that?

can you share complete workspace client script and also the screenshot?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

in the UI Action Client Checkbox is set to true and in the workspace client script  used g_aw.openRecord('table',sys_id) method. more detail please check the complete UI action screen shots.

find_real_file.png

 

find_real_file.png

 

find_real_file.png

find_real_file.png

 

 

 

 

find_real_file.png

Hi,

for UI action to work in workspace the entire code should be in workspace client script

only that code gets executed

So do this since workspace client script cannot use CartJS as that is server side

1) create client callable script include

2) add the code to create REQ, RITM inside that and use GlideAjax

3) once you return the ritm sys_id use this to open the record in new tab

Script Include: Client callable in customer service scope

var WorkspaceUtils = Class.create();
WorkspaceUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {


	createServiceRequest: function() {
		try {
			var reqSysid, ritmSysid;
			var itemId = 'a2a030391b800d10f6f7ddf2cc4bcbae'; // Catalog Item sys_id

			var tableName = this.getParameter('sysparm_table');
			var sysId = this.getParameter('sysparm_sysId');

			var rec = new GlideRecord(tableName);
			rec.get(sysId);

			// create new cart
			var cartId = gs.generateGUID();
			var cartGr = new sn_sc.CartJS(cartId); // Glide Record
			gs.info("Cart [" + cartId + "] has system id: " + cartGr.getCartID());
			var cntct = rec.contact.toString();
			var req_by = rec.opened_by.toString();
			var request = {
				'sysparm_id': itemId,
				'sysparm_quantity': '1',
				'variables': {
					'short_description': 'Test Catalog Item', // here list of variables for this item
					'requested_by': req_by,
					'customer_contact': cntct
				}
			};
			// add item to cart
			var cartDetails = cartGr.addToCart(request); // JS object
			for (var i in cartDetails) {
				gs.info("Cart Details: " + i + "=" + cartDetails[i]);
			}
			// check what is in the cart
			cartItems = cartGr.getCartItems(); // Glide Record
			// checkout cart
			var checkoutInfo = cartGr.checkoutCart(); // JS Object
			// check checkout response
			for (var k in checkoutInfo) {
				gs.info("CheckoutInfo: " + k + "=" + checkoutInfo[k]);
			}
			// Update Catalog Request
			var reqGr = new GlideRecord('sc_request');
			reqGr.get(checkoutInfo.request_id);
			reqGr.setValue('requested_for', rec.contact);
			reqGr.setValue('opened_by', rec.opened_by);
			reqGr.setValue('short_description', 'Part Return Request');
			reqGr.setValue('assignment_group', rec.assignment_group);
			reqGr.setValue('assigned_to', rec.assigned_to);
			reqGr.setValue('parent', rec.sys_id);

			reqSysid = reqGr.sys_id;
			reqGr.update();
			// Update ordered items tasks (RITMs)
			var itemGr = new GlideRecord('sc_req_item');
			while (cartItems.next()) {
				gs.info("Cart Item: " + cartItems.getValue('sys_id'));
				itemGr.initialize();
				itemGr.addQuery('request', checkoutInfo.request_id);
				itemGr.query();
				itemGr.setValue('requested_for', rec.contact);
				itemGr.setValue('opened_by', rec.opened_by);
				itemGr.setValue('assignment_group', rec.assignment_group);
				itemGr.setValue('assigned_to', rec.assigned_to);
				itemGr.setValue('parent', rec.sys_id);
				ritmSysid = itemGr.sys_id;
				itemGr.updateMultiple();
			}
			return rec.sys_id;
		} catch (ex) {
			gs.error("Exception: " + ex);
		}
	},

	type: 'WorkspaceUtils'
});

Workspace client script:

var ga = new GlideAjax('WorkspaceUtils');
ga.addParam('sysparm_name','createServiceRequest');
ga.addParam('sysparm_table', g_form.getTableName());
ga.addParam('sysparm_sysId', g_form.getUniqueValue());
ga.getXMLAnswer(callBackMethod);  

function callBackMethod(response) {  
	var url = '/sc_req_item.do?sys_id=' + response;
	open(url);
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

HARI KISHAN GVS
Mega Sage

Hi Hari,

Remove the action.setRedirectURL statement in the Script Block and remove script in the Workspace ClientScript block and Uncheck the Client Check box and add the following two lines in the script in the place of action.setRedirectURL.

action.openGlideRecord(itemGr);
action.setReturnURL(current);

 

Thank you!