如何解决可以在 sys_properties 中使用占位符吗?
是否可以创建一个 sys_property 值,例如:
This incident has been picked up by {person_name} for further investigation
然后在代码中使用类似的东西:
var assignee = "John Doe";
var prop = gs.getProperty('my_property_name');
// and now replace person_name with the value of assignee variable?
解决方法
您可以使用RecordToHTML
API,传统上它用于使用记录中的字段作为字符串字段的替换值,但是,在.setValue()
的帮助下,您可以绕过记录参数并指定您自己的值。请注意,要使字符串中的“变量”起作用,需要将其包裹在 ${}
而不是 {}
中:
var prop = gs.getProperty('my_property_name'); // "This incident has been picked up by ${person_name} for further investigation";
var assignee = "John Doe";
var rth = new RecordToHTML(null,null,prop);
rth.setValue("person_name",assignee); // replaces ${person_name} with "John Doe"
var result = rth.toString(); // "This incident has been picked up by John Doe for further investigation"
以上内容有点麻烦,因为它绕过了记录参数。如果您想使用另一种方法,您可以为此创建自己的函数。您可以首先将 assignee
放入包含键 person_name
并指向您的 assignee
值的对象中,然后使用函数替换 {<key>}
中的值,使用 {{1 }} 作为索引对象的键:
key
您还可以使用其他一些选项,这些选项也值得研究。一个是使用 RecordToHTML()
的第一个参数,另一个是 gs.getMessage()
。如果您有一个包含要从字符串中替换的字段的特定记录(请注意,您的字段需要包含在 function interpolate(str,obj) {
return str.replace(/{([^}]+)}/g,function(match,key) {
return obj[key] || match; // if we can't find the key,return the original `{key}`
});
}
var variableMap = {
"person_name": "John Doe" // assignee
};
var prop = gs.getProperty('my_property_name');
var msg = interpolate(prop,variableMap); // "This incident has been picked up by John Doe for further investigation"
中):
${}
还有 var rth = new RecordToHTML("incident",incidentSysId,"This incident has been picked up by ${assigned_to} for further investigation",false);
var result = rth.toString();
,它close,但它不允许在您的字符串中使用命名的“变量”,并要求您使用索引。每次调用时,它还会在 gs.getMessage()
表中执行表查询/查找,这可能有点矫枉过正:
sys_ui_message
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。