ServiceNow UI Action calling client and server code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-02-2017 10:57 PM
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/.
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.
I can't figure out where to put the pieces and parts to make this code complete and return "obj" to the catalogUTILS()???
//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);
}
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-27-2017 12:31 AM
you should render the form first, and wait for the button to be pressed again before you do the gsftSubmit(... part.
like this
function createTempCred(){
var tableName = g_form.getTableName();
var sysID = g_form.getUniqueValue();
if(view=='credential_view')
{
//this is triggered when you click the button while the dialog is open
gsftSubmit(null, g_form.getFormElement(), 'btnCreateCred'); //MUST call the 'Action name' set in this UI Action
}
else
{
//this is triggered when you click the button to show the dialog
//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
}
}
//add the other code here
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-27-2017 01:05 AM
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.
What you describe there sounds like a Record Producer.
Are you sure scripting is the only method possible to achieve this? It feels to me like you're trying to manually code functionality that already exists.