Service now flow designer access of changed_fields is returning undefined value in the script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 07:51 AM
Hi,
I am using the flow designer for one of the implementations. I am using changed_fields in script in the flow designer
it was working fine 2 months back, But now I am seeing an issue for retrieving it. The changed_fields value is return an undefined value.
var chgGr =fd_data.trigger.current;
var changed_fields = fd_data.trigger.changed_fields;
var changedFieldMap={};
for(var i=0;i<changed_fields.length;i++){
changedFieldMap[changed_fields[i].field_name]=true;
}
In the above code, its return undefined and throwing error. I can handle the error but, is there other way to change_fields value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2024 02:20 AM
Here's an alternative:
- Create an Action
- Action inputs (Label (Name) - Type):
- Table name (table_name) - Table Name
- * Record (record) - Document ID
- Modification count (modification_count) - Integer, default = -1
- Load journal fields (load_journal_fields) - True/False
- Add a Script step:
- Inputs (Name - Value):
- record - action 🢒 Record
- modificationCount - action 🢒 Modification count
- loadJournalFields - action 🢒 Load journal fields
- Script:
(function execute (inputs, outputs) { outputs.modification_index = getModificationCount(inputs); outputs.changed_fields = getChangedFields(inputs.record.getRecordClassName(), inputs.record.getUniqueValue(), outputs.modification_index); outputs.changed_fields_count = +outputs.changed_fields.length; function getChangedFields (tableName, uniqueValue, sysModCount) { var $gr = getGlideRecord(tableName, uniqueValue, sysModCount); return $gr == null ? [] : getFieldsOfTable(tableName).filter(isChangedFieldOf($gr)).filter(isNotSystemFieldOf($gr)).sort(); } function getFieldsOfTable (tableName) { return Object.keys(global.Schema.fromTable(tableName, ['*'])); } function getGlideRecord (tableName, uniqueValue, sysModCount) { var $hw = new sn_hw.HistoryWalker(tableName, uniqueValue); $hw.setWithChanges(true); $hw.setWithJournalFields(inputs.loadJournalFields); $hw.setWithVariables(false); return $hw.walkTo(sysModCount) ? $hw.getWalkedRecordCopy() : undefined; } function getModificationCount (inputs) { return inputs.modificationCount < 0 ? +inputs.record.sys_mod_count : +inputs.modificationCount; } function isChangedFieldOf ($gr) { return function isChangedField (fieldName) { return $gr.getElement(fieldName).changes(); }; } function isNotSystemFieldOf ($gr) { return function isNotSystemField (fieldName) { return !$gr.getElement(fieldName).getED().isAutoOrSysID(); }; } })(inputs, outputs);
- Output Variables (Label (Name) - Type):
- * Changed fields (changed_fields) - Array.String
- * Changed fields count (changed_fields_count) - Integer
- Modification index (modification_index) - Integer
- Action outputs:
- Changed field names (changed_field_names) - Array.String ⇐ step 🢒 Script step 🢒 Changed fields
- Changed fields count (changed_fields_count) - Integer ⇐ step 🢒 Script step 🢒 Changed fields count
- Modification index (modification_index) - Integer ⇐ step 🢒 Script step 🢒 Modification index
This would become a generic way to get the changed fields of a transaction, no matter the record or the transaction.
If the Modification count input is left -1 (default value) the current (most likely last) modification will be evaluated, otherwise the specified one. If the modification count is not valid, an empty array will be returned.
The same thing in pictures:
- The Action:
- A test sub-flow:
While this should solve getting the changed fields of any transaction, it should not be used in a loop as the history walker API extracts audit data from a bunch of very big audit tables.