Read script helper functions for LFDocumentContentBuilder
Summarize
Summary of LFDocumentContentBuilder Script Helper Functions
TheLFDocumentContentBuilderis a utility in ServiceNow designed to assist customers in extracting and managing translatable content from various artifacts, regardless of whether they use standard translation tables. These helper functions enable developers to systematically gather, organize, and process translatable strings within scripts, records, and multiple records, facilitating efficient localization workflows.
Show less
Key Functions and Their Practical Use
- initialize(version, targetLanguage, sysid, tableName): Acts as a constructor to initialize the builder with document version (e.g., "v1" for Xanadu release), target localization language, and artifact identifiers (sysid, table name).
- processString(string, groupName, label): Adds a single translatable string as a content element grouped and labeled for easy identification.
- processStringArray(stringArray, groupName, label): Handles multiple strings together, organizing them under the specified group and label.
- processScript(scriptContent, groupName, label): Extracts translatable strings specifically from script code inputs that use
getMessage,gs.getMessage, orgs.getMessageLangfunctions, enabling localization of dynamic script text. - processTranslatableFieldsForSingleRecord(glideRecord, groupName): Automatically extracts and creates elements for all translatable fields (including translatedfield, translatedtext, translatedhtml types) within a single GlideRecord, grouped for clarity.
- processTranslatableFieldsForMultipleRecords(tableName, encodedQuery, groupName): Fetches multiple records based on a query and processes all their translatable fields collectively.
- processElement(element): Allows manual addition of a prepared LFDocumentContentElement to the document content.
- build(): Completes the content building process and returns the LFDocumentContent object. Note that the older
getFinalJSONmethod is deprecated as of the Xanadu release.
Practical Examples
- Building Document Content: Instantiate the builder, process translatable fields for a single record, create fields and elements, add them to the builder, and finally call
build()to obtain the LFDocumentContent object ready for localization workflows. - Extracting Translated Data: Use the generated LFDocumentContent object to iterate over its elements and fields, retrieving original and translated values for further processing or storage.
Why This Matters to ServiceNow Customers
This set of helper functions streamlines the extraction and packaging of localized content from diverse sources within ServiceNow, reducing manual effort and errors. It supports complex scripts and multiple record scenarios, ensuring that all translatable content is identified and structured consistently. By using these APIs, customers can accelerate their localization projects, maintain translation quality, and integrate smoothly with ServiceNow’s localization framework.
The LFDocumentContentBuilder provides utility functions to build document content. Use these functions to write the logic to extract the translatable content from any artifacts regardless of them using standard tables or not for translations.
initialize (version, targetLanguage, sys_id, tableName)
| Name | Type | Description |
|---|---|---|
| Version | String | The version type of the document content to be generated. For Xanadu, this value is v1. |
| targetLanguage | String | The target language of the localization task document content. The target language is the same as the language available from the read script function argument. |
| Sys_id | String | The sys_id of the artifact record. |
| tableName | String | The table name of the artifact record. |
processString (string, groupName, label)
| Name | Type | Description |
|---|---|---|
| string | String | The value for which a document content entry is created. |
| groupName | String | The name of the group to which element should belong. |
| label | String | The unique label identifier for the created element. |
processStringArray (stringArray, groupName, label)
| Name | Type | Description |
|---|---|---|
| stringArray | List of Strings | The list of values for which document content entry is created. |
| groupName | String | The name of the group these strings belongs to. |
| label | String | The unique label identifier for these strings. |
processScript (scriptContent, groupName, label)
| Name | Type | Description |
|---|---|---|
| scriptContent | String | The Script to be processed to get the strings present as input of getMessage, gs.getMessage, or gs.getMessageLang. |
| groupName | String | The name of the group for this input. |
| label | String | The unique label identifier for this input. |
var documentContentBuilder = new LFDocumentContentBuilder("v1", language, sysId, tableName);
Var name = "name";
documentContentBuilder .processScript("gs.getMessage('Hello {0}, How are you', name)", "Script_Group", "Script_Label");processTranslatableFieldsForSingleRecord (glideRecord, groupName)
| Name | Type | Description |
|---|---|---|
| glideRecord | GlideRecord | The GlideRecord object to be processed for translatable fields. The GlideRecord should be pointing to the valid table record. |
| groupName | String | The name of the group for this input. |
processTranslatableFieldsForMultipleRecords (tableName, encodedQuery, groupName)
| Name | Type | Description |
|---|---|---|
| tableName | String | The table to fetch records. |
| encodedQuery | String | The encoded query for filtering records from the processTranslatableFieldsForSingleRecord table. |
| groupName | String | The name of the group for this input. |
processElement()
| element | LFDocumentContentElement | Element to be added to the document content. |
build()
Returns the LFDocumentContent object.
getTranslatableContent: function(params) {
var tableName = params.tableName;
var sysId = params.sysId;
var language = params.language;
var lfDocumentContentBuilder = new global.LFDocumentContentBuilder("v1", language, sysId, tableName);
// This will create a new object of LFDocumentContent and stores it internally
lfDocumentContentBuilder.processTranslatableFieldsForSingleRecord(gr, "Basic Info");
var field = LFDocumentContentHelper.createField(originalValue, translatedValue);
field.setTextType(LFDocumentContentHelper.PLAIN_TEXT_TYPE);
// All the other relevant attributes can be set
var element = LFDocumentContentHelper.createElement(groupName, label);
element.addField(field);
lfDocumentContentBuilder.processElement(element); // Adds element to the LFDocumentContent object
return lfDocumentContentBuilder.build(); // return the LFDocumentContent object
}
Sample script to extract data from the LFDocumentContent
saveTranslatedContent: function(documentContent) {
// LFDocumentContent object is passed as an argument to the saveTranslatedContent
var targetLanguage = documentContent.getLanguage();
var version = documentContent.getVersion();
var elements = documentContent.getElements(); // Array of LFDocumentContentElement objects
for (var idx = 0; idx < elements.length; idx++) {
var element = elements[idx];
var groupName = element.getGroupName();
var fields = element.getFields(); // Array of LFDocumentContentField objects
for (var fieldIdx = 0; fieldIdx < fields.length; fieldIdx++) {
var field = fields[fieldIdx];
var originalValue = field.getOriginalValue();
var translatedValue = field.getTranslatedValue();
// Get all the other required members and process them appropriately
}
}
}