How to access gs.getCurrentScopeName() from client side UI Actions?

xMTinkerer
Giga Expert

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.

7 REPLIES 7

prashantparbhan
ServiceNow Employee
ServiceNow Employee

Please see the attachment for more details.



I have created UI action with action name as "action_exexute".


Screen Shot 2015-03-03 at 8.33.52 AM.png



Thanks,


Prashant


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


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 );  


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




  1. //   Create a dialog form, set the submit action to 'null'    
  2. appPrefix = g_form.scope + '_';  
  3. 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