Save work Items via js-Job

Description
Przemek
Posts: 10
Joined: Mon Nov 12, 2012 2:45 pm

Save work Items via js-Job

Postby Przemek » Tue Jul 24, 2018 1:02 pm

Hi all,

I'm using the FMC-Workitem-Save-Extension which is really nice! I've created some jobs and they are running fine.
I have a pre-save-script that is calculating a custom currency field depending on the values from backlinked work items. It works fine but only if the respective work item is changed and therefore saved - just as it supposed to be.

Now I would like to to do the calculation also scheduled. So I've created a job in the scheduled job configuration, a new js-script and started to migrate my script so it would work not only for one work item like in the pre-save-script but also for all work items that matches a query.

It seams that everything is running fine BUT now I'm struggling to save the items.

Example:
The

Code: Select all

[...]
// Pass results from BigDecimal to Currency variables
var newCostEstimation = new Currency(costEstimation);
// Setting the value in Work Item
workItem.setCustomField("costEstimation", newCostEstimation);
[...]

is running fine with FMC but doesn't do anything in the new script

Code: Select all

[...]
// Pass results from BigDecimal to Currency variables
var newCostEstimation = new Currency(costEstimation);
// Setting the value in Work Item
electronicModule.setCustomField("costEstimation", newCostEstimation);
[...]


There is no error warning, the script just finishes with
INFO root - Script returned nothing
but the work items are not changed.

The used items

Code: Select all

workItem
and

Code: Select all

electronicModule
are from the same class
class com.polarion.alm.tracker.internal.model.WorkItem
, so what am I missing?

Has anyone some hints for me?

Thank you!

Przemek

dan7777777
Posts: 13
Joined: Wed May 17, 2017 5:08 pm

Re: Save work Items via js-Job

Postby dan7777777 » Thu Aug 02, 2018 1:10 pm

You have to open a transaction and save the work item. When you use the setCustomField method, you are only modifying the local object of the work item, not the one in your repo. To make the changes reflect on your repo, you would have something like this:

var txService = com.polarion.platform.core.PlatformContext.getPlatform().lookupService(com.polarion.platform.ITransactionService.class);

txService.beginTx();
//whatever changes you are trying to make go here, in this example it is changing the status:
wi.setEnumerationValue("status", "closed");
wi.save();
txService.commitTx();

calling wi.save() emulates a save on the work item (so it would check for required fields or field type mismatches here) but this must be through a transaction, so the actual change won't go through until the transaction is committed.

Przemek
Posts: 10
Joined: Mon Nov 12, 2012 2:45 pm

Re: Save work Items via js-Job

Postby Przemek » Wed Aug 29, 2018 12:36 pm

Yes, it's working with a small correction (without the ".class" in the third line):

Code: Select all

var txService = com.polarion.platform.core.PlatformContext.getPlatform().lookupService(com.polarion.platform.ITransactionService);

txService.beginTx();
//whatever changes you are trying to make go here, in this example it is changing the status:
wi.setEnumerationValue("status", "closed");
wi.save();
txService.commitTx();


Thank you and best regards!

Przemek


Return to “Polarion Application Lifecycle Management (ALM)”

Who is online

Users browsing this forum: No registered users and 9 guests