Acabei conseguindo resolver o meu problema.
function createDataset(fields, constraints, sortFields) {
//Cria as colunas
var dataset = DatasetBuilder.newDataset();
dataset.addColumn("processInstanceId");
dataset.addColumn("processId");
dataset.addColumn("version");
dataset.addColumn("cardDocumentId");
dataset.addColumn("movementDate");
dataset.addColumn("movementSequence");
dataset.addColumn("stateSequence");
dataset.addColumn("stateName");
dataset.addColumn("processTaskcolleagueId");
dataset.addColumn("processTaskcolleagueName");
var datasetworkflowProcess = DatasetFactory.getDataset('workflowProcess', null, constraints, sortFields);
var totalDeRegistros = datasetworkflowProcess.rowsCount;
//log.info(">>>>>>>>>>>>>>>>>>>>>>>> Total de Registros em [workflowProcess] ====> " + totalDeRegistros.toString());
for (var i = 0; i < totalDeRegistros; i++) {
var processInstanceId = datasetworkflowProcess.getValue(i, "workflowProcessPK.processInstanceId");
var companyId = datasetworkflowProcess.getValue(i, "workflowProcessPK.companyId");
var processId = datasetworkflowProcess.getValue(i, "processId");
var version = datasetworkflowProcess.getValue(i, "version");
var cardDocumentId = datasetworkflowProcess.getValue(i, "cardDocumentId");
//log.info(">>>>>>>>>>>>>>>>>>>>>>>> valor em [workflowProcessPK.processInstaceId] ====> " + processInstanceId);
//log.info(">>>>>>>>>>>>>>>>>>>>>>>> valor em [workflowProcessPK.companyId] ====> " + companyId);
var datasetProcessHistory = findProcessHistory(companyId, processInstanceId);
var movementSequence;
var processTaskcolleagueId;
var processTaskcolleagueName;
var stateSequence;
var activityName;
for (var h = 0; h < datasetProcessHistory.rowsCount; h++) {
try {
movementSequence = datasetProcessHistory.getValue(h, "processHistoryPK.movementSequence");
var datasetProcessTask = findprocessTask(companyId, processInstanceId, movementSequence);
processTaskcolleagueId = datasetProcessTask.getValue(0, "processTaskPk.colleagueId");
processTaskcolleagueName = findResponsableName(companyId, processTaskcolleagueId);
stateSequence = datasetProcessHistory.getValue(h, "stateSequence");
activityName = GetProcessStateName(companyId, processId, version, stateSequence);
} catch (error) {
log.info(">>>>>>>>>>>>>>>>>>>>>>>> Falha ao recuperar histórico ====> " + error);
}
dataset.addRow(new Array(
processInstanceId,
processId,
version,
cardDocumentId,
datasetProcessHistory.getValue(h, "movementDate"),
movementSequence,
stateSequence,
activityName,
processTaskcolleagueId,
processTaskcolleagueName
)
);
}
}
return dataset;
}
function findProcessHistory(companyId, processInstanceId) {
var constraint1 = DatasetFactory.createConstraint('processHistoryPK.companyId', companyId, companyId, ConstraintType.MUST);
var constraint2 = DatasetFactory.createConstraint('processHistoryPK.processInstanceId', processInstanceId, processInstanceId, ConstraintType.MUST);
var constraints = new Array(constraint1, constraint2);
return DatasetFactory.getDataset('processHistory', null, constraints, null);
}
function findprocessTask(companyId, processInstanceId, movementSequence) {
var constraint1 = DatasetFactory.createConstraint('processStatePK.companyId', companyId, companyId, ConstraintType.MUST);
var constraint2 = DatasetFactory.createConstraint('processTaskPK.processInstanceId', processInstanceId, processInstanceId, ConstraintType.MUST);
var constraint3 = DatasetFactory.createConstraint('processTaskPK.movementSequence', movementSequence, movementSequence, ConstraintType.MUST);
var constraints = new Array(constraint1, constraint2, constraint3);
return DatasetFactory.getDataset('processTask', null, constraints, null);
}
function findResponsableName(companyId, colleagueId) {
if (colleagueId == 'System:Auto') {
return "Atividade sem responsável definido";
} else if (colleagueId.indexOf('Pool:Group:') == 0) {
return "Atividade em pool por grupo";
} else if (colleagueId.indexOf('Pool:Role:') == 0) {
return "Atividade em pool por papel";
} else {
return findColleagueName(companyId, colleagueId);
}
}
function findColleagueName(companyId, colleagueId) {
var constraint1 = DatasetFactory.createConstraint('colleaguePK.companyId', companyId, companyId, ConstraintType.MUST);
var constraint2 = DatasetFactory.createConstraint('colleaguePK.colleagueId', colleagueId, colleagueId, ConstraintType.MUST);
var constraints = new Array(constraint1, constraint2);
var columns = new Array('colleagueName');
return DatasetFactory.getDataset('colleague', columns, constraints, null).getValue(0, "colleagueName");
}
function GetProcessStateName(companyId, processId, version, sequence) {
var constraint1 = DatasetFactory.createConstraint('processStatePK.companyId', companyId, companyId, ConstraintType.MUST);
var constraint2 = DatasetFactory.createConstraint('processStatePK.processId', processId, processId, ConstraintType.MUST);
var constraint3 = DatasetFactory.createConstraint('processStatePK.sequence', sequence, sequence, ConstraintType.MUST);
var constraint4 = DatasetFactory.createConstraint('processStatePK.version', version, version, ConstraintType.MUST);
var constraints = new Array(constraint1, constraint2, constraint3, constraint4);
var columns = new Array('stateSequence');
return DatasetFactory.getDataset('processHistory', columns, constraints, null).getValue(0, "stateSequence");
}