Service now flow designer access of changed_fields is returning undefined value in the script

vamshi123
ServiceNow Employee
ServiceNow Employee

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.

1 REPLY 1

-O-
Kilo Patron
Kilo Patron

Here's an alternative:

- Create an Action

- Action inputs (Label (Name) - Type):

  1. Table name (table_name) - Table Name
  2. * Record (record) - Document ID
  3. Modification count (modification_count) - Integer, default = -1
  4. Load journal fields (load_journal_fields) - True/False

- Add a Script step:

  1. Inputs (Name - Value):
    1. record - action 🢒 Record
    2. modificationCount - action 🢒 Modification count
    3. loadJournalFields - action 🢒 Load journal fields
  2. 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);​
  3. Output Variables (Label (Name) - Type):
    1. * Changed fields (changed_fields) - Array.String
    2. * Changed fields count (changed_fields_count) - Integer
    3. Modification index (modification_index) - Integer

- Action outputs:

  1. Changed field names (changed_field_names) - Array.String ⇐ step 🢒 Script step 🢒 Changed fields
  2. Changed fields count (changed_fields_count) - Integer ⇐ step 🢒 Script step 🢒 Changed fields count
  3. 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:17.png

     

    18.png

     

    19.png
  • A test sub-flow:20.png

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.