It's all about the answers!

Ask a question

Help with a custom work item attribute “Calculated Value”


Glyn Costello (1404555) | asked Jan 26 '22, 3:17 p.m.

 Hi,


Testing out a potential use of a calculated value attribute in RTC. I’m not much of a coder and I think I’m having issues with attribute types/Variable types. 

I have two attributes: rate (decimal) and qty (integer). I want to multiply the two in a scripted calculated value provider and save it as an attribute cost (decimal). 

Using workItem.getValue(attributeID) I’m struggling to extract the numbers to use in the simple calculation. Can anyone help?

Thanks

Glyn 

Accepted answer


permanent link
Ralph Schoon (63.5k33646) | answered Jan 28 '22, 2:55 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

 Support case was opened. Case is closed, there was an issue with the process configuration, as already hinted here.

This caused the null value for the attribute.

Ralph Schoon selected this answer as the correct answer

Comments
Glyn Costello commented Jan 28 '22, 6:05 a.m.
thank you for your help.

FYI, I was not editing the process source, only through UI. But hey ho.

Ralph Schoon commented Jan 28 '22, 6:08 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Does not matter how you edit, it is a process source at the end. 

Pleas share questions like this with support in the case next time. Thanks!

3 other answers



permanent link
Davyd Norris (2.8k217) | answered Jan 26 '22, 5:07 p.m.
Can you post your code up?

The core calculation should look something like this:

var result = parseFloat(workitem.getValue(rate)) * parseFloat(workitem.getValue(qty));
return Number.isInteger(result) ? result + ".0" : result.toString();

Since you specified that the result should be a decimal, the second line will check the result and add a decimal point into a result that's an integer



Comments
Glyn Costello commented Jan 26 '22, 5:13 p.m.

I’m pretty sure that will do it, thanks. I’ll try this tomorrow.  Cheers


Glyn Costello commented Jan 27 '22, 4:52 a.m.

not enough characters in the comment, so see post below


Glyn Costello commented Feb 02 '22, 8:11 a.m. | edited Feb 02 '22, 8:27 a.m.
Hi DAvyd, if the result has too many decimal places to store against the Work Item Attribute (which is happening), is there anyway to round to just two decimal places?

- I think I got it, I've changed ".toString()" to "toFixed(2)"

RTC seems to have a limit on the number of decimal places a decimal attribute can have. It's limited in the UI, but if it's a calculated value, it just fails to update the attribute

Davyd Norris commented Feb 02 '22, 5:03 p.m.
Yep that's the correct way to limit the number of places printed, but if you are using an actual float type as your attribute you should be able to leave off the formatting code completely and just write:

return result

One thing to bear in mind when you're doing floating point things in ELM. I found the float data types in RM/CCM/QM are very low precision for some reason, and if you need very high precision calculations you're actually better off storing the data as a string rather than a float. You then have to check for correct input, but the conversion for calculations is exactly the same as above.

I got messed up by this when I built a FMEA/FMECA RM extension widget for a client and they were typing in very small probabilities for their data. All my calculations were coming back as zero. Once I stored them as strings and did my own format conversion I had total control over the precision

permanent link
Ralph Schoon (63.5k33646) | answered Jan 27 '22, 3:09 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

 In addition to Davyds answer, please 


1. Have a look at https://www.w3schools.com/js/default.asp The site has helped me greatly with tips.
2. Learn how to use the Chrome Debugger

The debugger can be used this way:
Open the work item menu for your project area in Chrome. 
Inject ?debug=true into the URL in front of the #
e.g. https://elm.example.com:9443/ccm/web/projects/JKE Banking (Change Management)?debug=true#action=com.ibm.team.workitem.viewWelcome and run the tab again.
Use CTRL+SHIFT+I to open the developer tools.
Use the three vertical dots to the right of the cogwheel to change the location of the developer tools to the bottom.
Select the Sources tab.
Use a query or the Search Work Items to locate a work item or create one.
You will see two cloud icons, one shows your server and port e.g. elm.example.com:9443 and another that shows "(no domain)"; open the latter one. Find your calculated value script and open it. Set a breakpoint in the beginning.

When the script is run, the page will stop and you can step through the script. You can hover over variables to understand their form, types and values.

If you change the script and save it in Eclipse, make sure to completely reload the page in the browser to get the newest script version.

You can now debug your script (note does not work for default value scripts).



Comments
Glyn Costello commented Jan 27 '22, 4:55 a.m.

Thanks yes I'm already using W3 schools and browser debugging.


permanent link
Glyn Costello (1404555) | answered Jan 27 '22, 4:52 a.m.
edited Jan 27 '22, 4:53 a.m.
In the debugger, it seems to get the decimal attribute rate_1 ok, but is struggling to get the "Integer" attribute qty_1

dojo.provide("calculated.budgetTotal");

(function() {
    dojo.declare("calculated.budgetTotal", null, {

        getValue: function(attribute, workItem, configuration) {
       
        var r1 = workItem.getValue('rate_1');
        var q1 = workItem.getValue('qty_1');
        var tot1 = parseFloat(r1) * parseFloat(q1);

return tot1.toString();

        }
    });
})();

the debugger returns:

r1: (number)
q1: null (workItem.getValue: undefined)

I've double checked the attributeIDs, all seems to tie up correctly.

I tried changing q1 to a different (decimal) attribute and it still returns null. I can't seem to get a second attribute for the calculation.


Comments
Glyn Costello commented Jan 27 '22, 4:53 a.m.

I have checked and the "total" attribute has the rate_1 and qty_1 defined as "dependencies"


Ralph Schoon commented Jan 27 '22, 5:52 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Are you sure the work item has the attribute? Check usage in the work item type or create a new work item after (refresh) modifying the attribute/work item type.


If you get null, there is something wrong in the type definition/attribute definition/attribute ID. You have to solve the null issue first. BTW, I have played with decimal and you should get a value.


Glyn Costello commented Jan 27 '22, 5:56 a.m.

Hi, yes there is some odd issue.. I tried the script again adding two new attributes to the work item type, I created both in exactly the same way, both appear to have been added to the process config source correctly as far as I can see, however, one attribute is accessible via getValue() but the other one isn't.


Ralph Schoon commented Jan 27 '22, 8:15 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Make sure that you refresh/reload. I have seen cases where I did not get the changes activated for a while.  

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.