Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

This code is not running as expected.

Saud Ahmed
Giga Contributor

(function executeResyncTime() {

var FLOW_NAME = 'global.copy_of_sapmytime_out_bound_update'; // <-- paste exact name from code snippet
var TW_TABLE = 'task_time_worked';

var started = 0, failed = 0;

// All Time Worked entries linked to this Work Order Task
var tw = new GlideRecord(TW_TABLE);
tw.addQuery('task', current.getUniqueValue());
// Optional: skip entries already sent (confirm field name / values)
// tw.addEncodedQuery('u_time_worked_statusNOT INsubmitted,approved^ORu_time_worked_statusISEMPTY');
tw.orderBy('work_date');
tw.query();

while (tw.next()) {
try {
// Fresh record for each run
var rec = new GlideRecord(TW_TABLE);
rec.get(tw.getUniqueValue());

var inputs = {};
inputs['current'] = rec; // Time Worked record (not sys_id)
inputs['changed_fields'] = [];
inputs['table_name'] = TW_TABLE;

var ctx = sn_fd.FlowAPI.getRunner()
.flow(FLOW_NAME)
.inBackground() // Don't make the user wait on SAP calls
.withInputs(inputs)
.run();

started++;
gs.info('[Re-sync Time] WOT ' + current.getDisplayValue('number') +
' | time worked ' + rec.getUniqueValue() +
' | context ' + ctx.getContextId());
} catch (ex) {
failed++;
var msg = (ex && ex.getMessage) ? ex.getMessage() : String(ex);
gs.error('[Re-sync Time] WOT ' + current.getDisplayValue('number') +
' | time worked ' + tw.getUniqueValue() + ' | ' + msg);
}
}

if (started > 0) {
gs.addInfoMessage('Re-sync Time started for ' + started + ' time entr' + (started == 1 ? 'y' : 'ies') + '.');
} else if (failed == 0) {
gs.addInfoMessage('No time entries found on this Work Order Task to re-sync.');
}
if (failed > 0) {
gs.addErrorMessage(failed + ' time entr' + (failed == 1 ? 'y' : 'ies') +
' could not be re-synced. Check the system log.');
}

action.setRedirectURL(current);
})();

0 REPLIES 0