It's all about the answers!

Ask a question

script for adding two attributes in RTC


Naveen Chandani (4811922) | asked Jun 14 '12, 5:18 a.m.
retagged Jun 14 '12, 3:00 p.m. by Evan Hughes (2.4k1018)
I have created 3 customized attributes in rtc (attrib 1, attrib 2, attrib 3) for the task workitem type. The datatype for all the 3 is Integer.
I want the sum of the two attributes (attrib 1, attrib 2) to be displayed automatically in the 3rd attribute (attrib 3). Could you please help me with the required script.

Accepted answer


permanent link
Dinesh Kumar (4.0k413) | answered Jun 14 '12, 12:39 p.m.
JAZZ DEVELOPER
one more way to do the same:

dojo.provide("org.scripts.addTwoArttibs");
dojo.require("dojo.number");
       
(function() {

dojo.declare("org.scripts.addTwoArttibs", null, {

    getValue: function(attributeId, workItem, configuration) {

        var res = 0;
       
           var firstVal = dojo.number.parse(workItem.getValue("rcstlt.ms.firstInt"), {type:'decimal'});
        var secondVal = dojo.number.parse(workItem.getValue("rcstlt.ms.secondInt"), {type:'decimal'});
       
        res = firstVal + secondVal;
       
        return res;
       
    }
});
})();
Ralph Schoon selected this answer as the correct answer

Comments
Naveen Chandani commented Jun 17 '12, 3:15 p.m.

Thanks a lot Ralph!!!!

I really appreciate your quick response. The script ran perfectly fine with the Integers but now I realised that the data type of my attributes is string. Could you please help me in writing a similar script for string variables.

3 other answers



permanent link
Ralph Schoon (56.7k23642) | answered Jun 14 '12, 6:45 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
I think I have posted this very same answer like a few days ago already. However, I wasn't able to find it myself, so here goes again. Make sure to add the other attributes to the dependent list of attributes for the attribute that is calculated.

dojo.provide("org.example.workitems.providers.CalcInteger");

dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("dojo.date");
dojo.require("dojo.date.stamp");


(function() {
var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes;

dojo.declare("org.example.workitems.providers.CalcInteger", null, {

    getValue: function(attributeId, workItem, configuration) {

        console.log("Start..");
        var text="";
        var out = "Values:\n";
       
        // for integer there should always be a value 0 by default
        var aValue = workItem.getValue("calcInputInt1");
        text = "Value: " + aValue;
        console.log(text);

        // for integer there should always be a value 0 by default
        var aValue2 = workItem.getValue("calcinputint2");
        text = "Value2: " + aValue2;
        console.log(text);
       
        // Was: return aValue+aValue2;
        // parseInt(addMarginVal)+parseInt(costSavingVal)-parseInt(investVal);
        return parseInt(aValue)+parseInt(aValue2);
    }
});
})();



permanent link
Ralph Schoon (56.7k23642) | answered Jun 18 '12, 4:36 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Hi Naveen,

please rate the answers if they are useful. String wise I have a similar solution. I can't test it right now. If it concatenates instead of adding, I would search the internet on how java script allows to convert to number or to calculate based on numbers. you would probably also want to add a validation, so that you actually restrict input to valid numbers. My code:

dojo.provide("org.example.workitems.providers.CalcString");

dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("dojo.date");
dojo.require("dojo.date.stamp");


(function() {
var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes;

dojo.declare("org.example.workitems.providers.CalcString", null, {

    getValue: function(attributeId, workItem, configuration) {

        console.log("Start..");
        var text="";
        var out = "Values:\n";
       
        var aValue = workItem.getValue("calcinputstring1");
        if(aValue=null){
            aValue="";
        }
        text = "Value: " + aValue;
        console.log(text);

        var aValue2 = workItem.getValue("calcinputstring2");
        if(aValue2=null){
            aValue2="";
        }
        text = "Value2: " + aValue2;
        console.log(text);

        return aValue+aValue2;
    }
});
})();


Comments
Naveen Chandani commented Jun 18 '12, 5:46 a.m.

Thanks Ralph but this script doesen't concatenates either. My scenario is that I have 3 attributes of string data type (attribute4, attribute5, attribute6). I would enter the numeric value in attribute4 & 5. I want the sum of the two to be populated automatically in attribute6.

I agree with you that I need to add a validator as well to ensure that the user enters the no. only.


Ralph Schoon commented Jun 18 '12, 6:07 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Naveen,

what do you see?

I have tested this and it worked, as far as I know. You have to make sure to add the attributes 4&5 to the dependent attributes list of attribute 6. What does the log show? I assume you have read https://jazz.net/wiki/bin/view/Main/AttributeCustomization careful and recognized where the different log files are located. You have to enable scripting in the RTC/CCM advanced sttributes. Did you do that?


Naveen Chandani commented Jun 18 '12, 8:30 a.m.

Ralph before executing the script I made the following changes: 1. changed "calcinputstring1" to "attribute4" and "calcinputstring2" to "attribute5". 2. added the attribute 4 & 5 in the dependency list while creating attribute6. scripting is already enabled becuase the first script is working for me. log:

!ENTRY com.ibm.team.rtc.common.scriptengine 1 0 2012-06-18 17:50:22.618 !MESSAGE LOG: Start..

!ENTRY com.ibm.team.rtc.common.scriptengine 1 0 2012-06-18 17:50:22.620 !MESSAGE LOG: Value: null

!ENTRY com.ibm.team.rtc.common.scriptengine 1 0 2012-06-18 17:50:22.622 !MESSAGE LOG: Value2: null


Naveen Chandani commented Jun 18 '12, 8:31 a.m.

i can see 0.0 coming by default for attribute 6 and it remains the same after entering the values for attribute 4 & 5.


Ralph Schoon commented Jun 18 '12, 8:47 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Hi, i would try to add more console.log statements. I don't see the start for instance. I would suggest to play around with it a little more.


Naveen Chandani commented Jun 19 '12, 3:01 a.m.

Hi Ralph, The script will work if we comment the IF loop (line 13) and parse the values as integer using parseInt in return function (line 24). Thanks for your help!!!

showing 5 of 6 show 1 more comments

permanent link
Ralph Schoon (56.7k23642) | answered Jun 19 '12, 3:37 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Hi Naveen,

if this answer was correct/helpful, please cast your vote and mark it. You might also want to provide your final solution.

Your answer


Register or to post your answer.