Expand my Community achievements bar.

SOLVED

Update workflow xml

Avatar

Level 1
Hi I have previously run a workflow to update deliveries using nms.delivery.load() and then some find/replace type js to fix what I wanted. I was hoping to do something similar to some workflows - to remove keepResult="true" from some workflow xmls in bulk. Is there a way to do this? it's within the initial workflow tag rather than in activities or something further down so I am not sure how to edit it. Many thanks Stephen
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @StephenWa4 

 

You can follow Parthasarathy tutorial or just download the package I added directly.

Keep interim populations 

Thanks



David Kangni

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @StephenWa4 

 

You can follow Parthasarathy tutorial or just download the package I added directly.

Keep interim populations 

Thanks



David Kangni

Avatar

Level 2

@StephenWa4 
Use Case:

You want to remove keepResult="true" from the root <workflow> tag in multiple workflows in bulk.

Approach:
1.  Use xtk:workflow schema to query workflows (by internal name, folder, etc.)

2.  Load each workflow's XML.

3.  Remove the keepResult attribute from the root <workflow> tag (if present).

4.  Save the updated XML back to the workflow.

Sample JavaScript Code (to run inside a JavaScript activity in a workflow):
//Java
// STEP 1: Query workflows based on internal name pattern or other conditions

var workflows = [];
var query = NLWS.xtkQueryDef.create({
queryDef: {
schema: "xtk:workflow",
operation: "select",
select: {
node: [
{ expr: "@id" },
{ expr: "@name" },
{ expr: "@internalName" }
]
},
where: {
condition: {
expr: "@internalName LIKE 'myWorkflowPrefix%'" // Replace with your desired filter
}
}
}
});
var result = query.ExecuteQuery();
workflows = result.workflow;

// STEP 2: Iterate through workflows and modify XML

for each (var wf in workflows) {
var wfId = wf["@id"];
var workflow = NLWS.xtkWorkflow.load(wfId);

var dom = new XML(workflow.xml);

// Check and remove keepResult="true" if present

if (dom.@keepResult == "true") {
logInfo("Removing keepResult from workflow: " + wf.@internalName);
delete dom.@keepResult;

// Save modified XML back to the workflow
workflow.xml = dom.toXMLString();
NLWS.xtkWorkflow.save(workflow);
} else {
logInfo("No keepResult to remove in workflow: " + wf.@internalName);
}
}

Recommendations:
-  Replace 'myWorkflowPrefix%' with an actual filter matching the workflows you want to update.

-  Consider backing up the workflow XML or cloning workflows before performing bulk updates.

-  Add checks to ensure the workflow is not currently running (status = paused or stopped), if needed.

 

Have a nice day. 
Thanongdach