Expand my Community achievements bar.

SOLVED

Restart workflow (Javascript)

Avatar

Level 6

Hi team, 

 

I've applied the JavaScript code to initiate the restart of my workflow. Despite confirming the real status and observing it as "finished," whenever the JavaScript activity is executed, it consistently displays "No event," despite the workflow already being in a finished state. Can someone clarify why this inconsistency is happening?

 

var query = xtk.queryDef.create(
<queryDef schema = {vars.targetSchema} operation = "select">
<select>
<node expr="@state"/>
</select>
</queryDef>
);
var record = query.ExecuteQuery();
var status = vars.state;

if (status=="Finished"){
xtk.workflow.Restart("workflowInternalName");
task.postEvent(task.transitionByName("Ok"));
}
else {
task.postEvent(task.transitionByName("No"));
}

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 6

Hi @AkshayAnand , 

 

Thank you for your response. I managed to resolve the issue by updating my JavaScript activity to below, and everything is functioning correctly now.

 

var query = xtk.queryDef.create(
<queryDef schema = {vars.targetSchema} operation = "select">
<select>
<node expr="@status"/>
<node expr="@state"/>
<node expr="@internalName"/>
</select>
</queryDef>
);

var workflowsToStart = query.ExecuteQuery();
var stats = vars.status;

for each (var variable in workflowsToStart) {
vars.workflowID = variable.@internalName;
if (stats=5){
xtk.workflow.Restart(vars.workflowID);
task.postEvent(task.transitionByName("Ok"));
}
else if(stats=13){
xtk.workflow.Restart(vars.workflowID);
task.postEvent(task.transitionByName("Ok"));
}

else {
task.postEvent(task.transitionByName("No"));
}

}

View solution in original post

2 Replies

Avatar

Community Advisor

Hi @rvnth 

 

I hope you are referring the variable "Status" as the workflow state and your target schema is xtk:workflow. Hence could you kindly try using the below code.

 

var query = xtk.queryDef.create(
<queryDef schema = "xtk:workflow" operation = "select">
<select>
<node expr="@state"/>
</select>
<where>
<condition expr="[@internalName] = 'workflowInternalName'"/>
</where>
</queryDef>
);
var record = query.ExecuteQuery();
logInfo(record);

var status = record.@state;

if (status=="Finished"){
xtk.workflow.Restart("workflowInternalName");
task.postEvent(task.transitionByName("Ok"));
}
else {
task.postEvent(task.transitionByName("No"));
}

 

Regards

Akshay

Avatar

Correct answer by
Level 6

Hi @AkshayAnand , 

 

Thank you for your response. I managed to resolve the issue by updating my JavaScript activity to below, and everything is functioning correctly now.

 

var query = xtk.queryDef.create(
<queryDef schema = {vars.targetSchema} operation = "select">
<select>
<node expr="@status"/>
<node expr="@state"/>
<node expr="@internalName"/>
</select>
</queryDef>
);

var workflowsToStart = query.ExecuteQuery();
var stats = vars.status;

for each (var variable in workflowsToStart) {
vars.workflowID = variable.@internalName;
if (stats=5){
xtk.workflow.Restart(vars.workflowID);
task.postEvent(task.transitionByName("Ok"));
}
else if(stats=13){
xtk.workflow.Restart(vars.workflowID);
task.postEvent(task.transitionByName("Ok"));
}

else {
task.postEvent(task.transitionByName("No"));
}

}