const codeengine = require('codeengine'); async function getMostRecentlyCompletedUserTask() { const { entityType, entityDetails } = codeengine.getExecutionInformation(); if (!entityDetails || entityType !== "WORKFLOW") { throw `Cannot get most recently completed user task for function executions not ran in a workflow`; } const { modelId, version, instanceId } = entityDetails; const query = { "status": ["COMPLETED"], "sourceSystem": ["ODYSSEY"], "query": { "sourceInfo.modelId": modelId, "sourceInfo.modelVersion": version, "sourceInfo.instanceId": instanceId } }; const url = `/api/queues/v1/tasks/list?limit=1&orderBy=completedOn&direction=DESC`; const tasks = await codeengine.sendRequest("POST", url, query); const task = tasks[0]; if (!task) throw `Could not find a recently completed task for this workflow instance`; return task; }