How to access gs.getCurrentScopeName() from client side UI Actions?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2015 12:00 PM
I must say I like this "gs.getCurrentScopeName()" function as it means I don't have to hardcode the scope name in all my scripts. However, the "gs" object is not accessible in UI Actions with the client box checked. Any ideas on how to access this value? I suppose using g_scratchpad would work, but checking if there are any "native" avenues rather than having to create a server side record just to set this value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2015 07:06 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2015 08:54 AM
Hey Prashant,
Ok, I see what it is doing there. I'm not sure where it went, but the email notification you had this link: http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/ that has some background. However, in that case they are putting stuff into the "current" record, and you are posting info to the messages. I now realize I didn't put my question into context. Sorry!
I'm trying to access gs.getCurrentScopeName so that I can then fire up a GlideDialogForm on a scoped table. So I need to get the scope name so that I can properly pass it to create the form. For example:
// Create a dialog form, set the submit action to 'null'
var dialog = new GlideDialogForm( 'Go Do Task Stuff', appPrefix + 'my_task', null );
// Set the sys_id to -1 to create a new record
dialog.setSysID('-1');
// Specify a form view
dialog.addParm('sysparm_view', 'default');
// Leave the related lists on the form visible
dialog.addParm('sysparm_form_only', 'false');
Where appPrefix should be the result of gs.getCurrentScopeName() +'_'.
I played with your example a bit, but I think the variable is getting lost:
// Maybe try a "full" scope?
var appPrefix = 'top var';
function doStuff( item ) {
console.log( 'appPrefix: ' + appPrefix );
console.log( 'serverReopenVar1: ' + serverReopenVar1 );
}
if( typeof window == 'undefined' )
serverReopen();
function serverReopen() {
gs.addInfoMessage( 'hi' );
gs.info( 'In serverReopen' );
appPrefix = gs.getCurrentScopeName() + '- serverReopen';
serverReopenVar1 = 'serverReopen';
}
And actually, after putting in that gs.info message, I don't think it is even running the 'serverReopen" function. The console output is:
appPrefix: top var
Uncaught ReferenceError: serverReopenVar1 is not defined
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2015 11:22 PM
Yea, g_scratchpad is the a way to go. I created a Business Rule that runs on display with the following script:
function onDisplay(current, g_scratchpad) {
// The gs variable is not available in client UI Actions. So we need this here.
g_scratchpad.appPrefix = gs.getCurrentScopeName();
}
Then, in the client side UI Action, I reference it like so:
// Create a dialog form, set the submit action to 'null'
appPrefix = g_scratchpad.appPrefix + '_';
var dialog = new GlideDialogForm( 'Go Do Task Stuff', appPrefix + 'my_task', null );
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2015 11:28 PM
Hi Travis,
But like Bobby mentioned below gs.getCurrentScopeName() is going to give you the the scope, you currently have selected via the application picker not the scope of the form you are on. You can use following script also
client side UI Action
- // Create a dialog form, set the submit action to 'null'
- appPrefix = g_form.scope + '_';
- var dialog = new GlideDialogForm( 'Go Do Task Stuff', appPrefix + 'my_task', null );
Could you please double check, your solution by changing a scope from application picker.
Thanks,
Prashant