How do I get a UI action to return values from a GlideDialogWindow?

will_smith
Mega Guru

I have a UI action that I created and I am attempting to use a GlideDialogForm to get and return user input to the UI action. I am new to scripting in general (and ServiceNow in particular) and can't figure out where to stick the paces of my code to make it flow. The below gives me an error. I have reviewed mark.stanger's excellent site SNGuru article http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/ as well as following this thread http://https://community.servicenow.com/thread/147086; which is similar to the answered thread https://community.servicenow.com/thread/191811

Thank you in advance for all of your help with my issue. I thought I was done with this UI action, and then I found out it wasn't returning anything to the script include the previous admin wrote.

Below is the overview of how this should work

Overview:

  a. ui action calls form that asks for data.

  b. form returns entered data to ui action script when user hits OK

  c. ui action script then takes entered data and does things to create the new record.

        - new record would include a lookup with the correct KB

        - new record u_parent would contain the sys_id of the created from record.

        - user entered data would be populated.

WARNING at line 37: Unreachable 'var' after 'return'.

//Client-side 'onclick' function

function newCredentialForm(){

  //Call the UI Action and skip the 'onclick' function

  gsftSubmit(null, g_form.getFormElement(), 'newCredentialForm'); //MUST call the 'Action name' set in this UI Action

}

//Code that runs without 'onclick'

//Ensure call to server-side function with no browser errors

if(typeof window == 'undefined')

var tableName = g_form.getTableName();

var sysID = g_form.getUniqueValue();

//Get the values from the Incident Table so we can write-back to it

var comments = g_form.getValue('comments');

var short_description = g_form.getValue('short_description');

var work_notes = g_form.getValue('work_notes');

//Create and open the dialog form

var dialog = new GlideDialogForm('Create Temporary Credential', 'u_temp_credential', callbackFunct);

dialog.setSysID(-1); //Pass in sys_id to edit existing record, -1 to create new record

dialog.addParm('sysparm_view', 'credential_view'); //Use the Credential view of the form

dialog.addParm('sysparm_form_only', 'true'); //Remove related lists

dialog.render(); //Open the dialog window

runServer_SideCode();

//Server-side function - not a business rule

function runServer_SideCode(){

  //returning data from the temp cred form as an array

  var obj = {};

  obj.application = g_form.getValue('u_application');

  obj.username = g_form.getValue('u_for_user.user_name'); //u_temp_credential.u_for_user => sys_user (table).user_name (userID field)

  obj.password = g_form.getValue('u_temp_password');

  return obj;

  var utils = new CatalogUTILS();

  var uri = utils.setTempCredential(current.caller_id,obj.application,obj.username,obj.password,current.sys_id);

  current.comments = 'to access your new credentials blah blah blah https://' + gs.getProperty('instance_name') + uri;

  gs.addInfoMessage('You did it, server super-star!');

}

/*

//this is my callback function. I am taking the username and using the setValue to place it in the additional comments field.

function callbackFunct(action, sys_id, table, displayValue) {

  //var credURL = 'View your temporary password at the following URL:\n https://wesco.service-now.com/u_temp_credential_list.do'

  //g_form.setValue('comments',credURL);

  //then in the ui script you would do this with the return value (server-side code)

}

*/

4 REPLIES 4

Bharat23
Kilo Guru

You have return obj; at line 40 which stops the execution of the function and returns the control back to the caller, is it intentional...if yes, what is the purpose writing lines of code after line 40 because they are unreachable?


Joe Wilmoth
ServiceNow Employee
ServiceNow Employee

Hi William,



You're returning the object at line 40. So, you ARE returning something from the UI Action. You're just not doing anything with what is returned. You need to move code around or delete line 40 so the rest of the function will run. Once you define the obj variable, it is in scope and available. You've added the properties to obj, so all you need to do if you are wanting to use obj in scope of this function, is to not return obj. If you delete line 40, obj will exist and have the properties you've assigned to it.



I would move the 'obj' object and property assignment out of the server side code function — g_form isn't server side code for one — and just run the query in the server side code function. Also, obj is not an Array. It is an object with key:value pairs or 'properties'. An array would be a collection of data, so an Array could contain many objects or an Object could contain Arrays of information in a property. Hope that helps or makes some sort of sense.



Thank you,


Joe


will_smith
Mega Guru

Thanks for the help and explanations   joe.wilmoth, but I have some further questions. I removed the return out of the code, and you suggested to run the query in the server side code. I am having some trouble grasping how to do that in the new window of the glide dialog window that gets created. How do I query that instance of the window and get the values to then return those values to the script include I have written?


will_smith
Mega Guru

Here's my updated code, but it still doesn't seem to be returning the values back to catalogUTILS()   script include. Here is some advice I received from a friend, hope it helps...thank you everyone for taking a look into this for me! I need to get it finished by today.



//You are probably best returning data from the form as an object like this:
var obj = {}
obj.application = <user input>;
obj.username = <user input>;
obj.password = <user input>;
return obj;


//then in the ui script you would do this with the return value.
var utils = new CatalogUTILS();
var uri = utils.setTempCredential(current.caller_id,obj.application,obj.username,obj.password,current.sys_id);
current.comments = 'to access your new credentials blah blah blah https://' + gs.getProperty('instance_name') + uri;



thats why I wrote the setTempCredenial method in catalog utils for when you got to that point.


//////////////////////////////


//Client-side 'onclick' function


function createTempCred(){


  var tableName = g_form.getTableName();


  var sysID = g_form.getUniqueValue();



  //Create and open the dialog form


  var dialog = new GlideDialogForm('Create Temporary Credential', 'u_temp_credential'); //callbackFunct no longer necessary


  dialog.setSysID(-1); //Pass in sys_id to edit existing record, -1 to create new record


  dialog.addParm('sysparm_view', 'credential_view'); //Use the Credential view of the form


  dialog.addParm('sysparm_form_only', 'true'); //Remove related lists


  dialog.render();//Open the dialog window




  //Call the UI Action and skip the 'onclick' function


  gsftSubmit(null, g_form.getFormElement(), 'btnCreateCred'); //MUST call the 'Action name' set in this UI Action


  alert(obj.username);


}




//Code that runs without 'onclick'. Ensures call to server-side function with no browser errors


if(typeof window == 'undefined')


  runServer_SideCode();




//Server-side function - not a business rule


function runServer_SideCode(){


  //returning data from the temp cred form


  var obj = {};


  obj.application = current.u_application;


  obj.username = curent.u_for_user.user_name; //u_temp_credential.u_for_user => sys_user (table).user_name (userID field)


  obj.password = current.u_temp_password;



  //testing the dot walking for the username





  var utils = new CatalogUTILS();


  var uri = utils.setTempCredential(current.caller_id,obj.application,obj.username,obj.password,current.sys_id);



  current.comments = 'to access your new credentials blah blah blah https://' + gs.getProperty('instance_name') + uri;


  gs.addInfoMessage('You did it, server super-star!');


  //action.setRedirectURL(current);


}