能否在sys_properties中使用占位符?以及如何通过代码替换系统属性值中的占位符内容?
Answers to Your ServiceNow sys_properties Placeholder Questions
Great questions! Let's break this down clearly for you:
1. Is it allowed to use placeholders in sys_properties?
Absolutely! ServiceNow doesn’t restrict you from including placeholder strings like {person_name} in sys_property values. The property is stored as plain text, so you can use any placeholder syntax that makes sense for your use case—just ensure your later script logic can recognize and replace those placeholders correctly.
2. Can you create the specified sys_property and replace the placeholder via code?
Yes, this is totally feasible, and your initial approach is on the right track. Here’s a polished version of the code that will properly replace the placeholder:
var assignee = "John Doe"; var prop = gs.getProperty('my_property_name'); // Replace the {person_name} placeholder with the assignee's name var formattedMessage = prop.replace(/{person_name}/g, assignee); // Example usage: log the formatted message gs.info(formattedMessage); // Outputs: "此事件已由John Doe接手进行进一步调查"
A few useful tips:
- The
gflag in the regular expression ensures all instances of{person_name}in the property value get replaced (not just the first one). If you only expect a single occurrence, you can omit thegflag. - For scenarios with multiple placeholders (e.g., adding
{ticket_id}or{incident_date}later), use a more scalable approach with a replacement object:
This way, you can expand your placeholders without rewriting core replacement logic.var replacements = { person_name: "John Doe", ticket_id: "INC0012345", incident_date: gs.nowDateTime() }; var prop = gs.getProperty('my_property_name'); var formattedMessage = prop.replace(/{(\w+)}/g, function(match, key) { // Return the replacement value, or keep the original placeholder if no match is found return replacements[key] || match; });
内容的提问来源于stack exchange,提问作者asprin
相关产品推荐
相关产品推荐

