可以在 sys_properties 中使用占位符吗?
Possible to use placeholders in 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, traditionally it's used for using the fields from a record as the replacement values for the string fields, however, with the help of .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
值的对象中,然后使用一个函数使用 [=22] 替换 {<key>}
中的值=] 作为索引对象的键:
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"
还有一些其他选项可供您使用,它们可能会满足您的需求,这些选项也值得研究。一个是使用 RecordToHTML()
and another being gs.getMessage()
的第一个参数。 RecordToHTML 也可以很好,如果你有一个特定的记录,其中包含你想从你的字符串中替换的字段(请注意,你的字段需要包装在 ${}
中):
var rth = new RecordToHTML("incident", incidentSysId, "This incident has been picked up by ${assigned_to} for further investigation", false);
var result = rth.toString();
还有 gs.getMessage()
,它来自 close,但它不允许在您的字符串中命名“变量”,并且需要您使用索引。它还会在每次调用时在 sys_ui_message
table 中执行 table 查询/查找,这可能有点矫枉过正:
// prop = This incident has been picked up by {0} for further investigation
var msg = gs.getMessage(prop, [assignee]);
是否可以创建一个 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, traditionally it's used for using the fields from a record as the replacement values for the string fields, however, with the help of .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
值的对象中,然后使用一个函数使用 [=22] 替换 {<key>}
中的值=] 作为索引对象的键:
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"
还有一些其他选项可供您使用,它们可能会满足您的需求,这些选项也值得研究。一个是使用 RecordToHTML()
and another being gs.getMessage()
的第一个参数。 RecordToHTML 也可以很好,如果你有一个特定的记录,其中包含你想从你的字符串中替换的字段(请注意,你的字段需要包装在 ${}
中):
var rth = new RecordToHTML("incident", incidentSysId, "This incident has been picked up by ${assigned_to} for further investigation", false);
var result = rth.toString();
还有 gs.getMessage()
,它来自 close,但它不允许在您的字符串中命名“变量”,并且需要您使用索引。它还会在每次调用时在 sys_ui_message
table 中执行 table 查询/查找,这可能有点矫枉过正:
// prop = This incident has been picked up by {0} for further investigation
var msg = gs.getMessage(prop, [assignee]);