我创建了一个 UI 操作,我正在尝试使用 GlideDialogForm 来获取用户输入并将其返回到 UI 操作。我对一般脚本编写(尤其是 ServiceNow)很陌生,无法弄清楚在哪里保持代码的节奏以使其流畅。下面给了我一个错误。我回顾了 mark.stanger 的优秀网站 SNGuru 文章 http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/.
提前感谢您对我的问题提供的所有帮助。我以为我已经完成了这个 UI 操作,然后我发现它没有向脚本返回任何内容,包括以前的管理员编写的内容。
以下是其工作原理的概述 概述: A。 ui 操作调用请求数据的表单。 b.当用户点击“确定”时,表单将输入的数据返回到 ui 操作脚本 C。然后,ui 操作脚本获取输入的数据并执行创建新记录的操作。 - 新记录将包括使用正确 KB 的查找 - 新记录 u_parent 将包含从记录创建的 sys_id。 - 用户输入的数据将被填充。
我不知道在哪里放置各个部分以使此代码完整并将“obj”返回到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);
}
一旦开始在 UI 操作中使用 onClick,脚本就会变成客户端脚本,并且不会运行服务器端代码。
要使“runServer_SideCode”方法起作用,需要使用Ajax。
//Move this to a Script include and call this from your UI Action using Ajax
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);
基于 servicenowguru.com 上的这篇文章,可以在一个 UI Action 中运行客户端和服务器端代码,而无需 Ajax。
根据该文章,它利用了以下事实:UI 操作可以配置为在客户端或服务器上运行脚本。在前一种情况下,“客户端”复选框被选中,而在后一种情况下,它被清除,但在任何一种情况下,UI 操作始终发送到服务器。
客户端代码在“Onclick”字段中命名的函数中定义。当用户单击 UI 操作按钮/链接时,将调用此函数。但是,在通往服务器的途中,无论是否选中“客户端”复选框,整个 UI 操作脚本都会执行。这意味着 UI 操作中未包含在函数中的任何脚本都将在服务器端运行。
下面是文章中所示的 UI Action 示例的简化版本。
UI 操作字段值:
名称:重开事件动作名称:reopen_incident
客户:是的
表单按钮:True
Onclick:重新打开();
条件:current.state == 6
UI 动作脚本:
//Client-side 'onclick' function referenced in "Onclick" field
function reopen(){
//This example client-side code only ensures comments are entered
if (g_form.getValue('comments') == '') {
//Set comments mandatory and show a new field message
g_form.hideFieldMsg('comments');
g_form.setMandatory('comments', true);
g_form.showFieldMsg('comments','Comments are mandatory','error');
return false; //Abort submission
}
//Below must call the 'Action name' set in "Action name" field
gsftSubmit(null, g_form.getFormElement(), 'reopen_incident');
}
/*
Below IF-statement runs on the server because it is not encapsulated
by a function. The condition in the IF-statement ensures that it is not
called within a browser window (i.e. on client-side).
*/
if (typeof window == 'undefined')
serverReopen();
function serverReopen(){
//Set the 'State' to 'Active', update and reload the record
current.state = 2;
current.update();
gs.addInfoMessage('Incident ' + current.number + ' reopened.');
action.setRedirectURL(current);
}