It's all about the answers!

Ask a question

RTC 5.0.1- Script to update a custom field based on Status


V Niranjan (12545173) | asked May 08 '15, 3:38 a.m.

Hi All

I have a scenario where I have to update a custom field based on Status of the Work Item.

For e.g. If Status is New then the custom field (in this case % completion ) should have the value 0.

Similarly if Status is "In Progress"  then the custom field value should be 30 and so on.

Is there a script for the same. Pleas let me know. It is bit URGENT.

Regards

Niranjan V

One answer



permanent link
Dinesh Kumar B (4.1k413) | answered May 08 '15, 6:29 a.m.
JAZZ DEVELOPER
edited May 08 '15, 6:35 a.m.
Hi Niranjan,

I believe the trouble is to find the ID to compare against in your script.

The best way is to use a console.log on the state attribute - as in
      console.log(workItem.getValue(WorkItemAttributes.STATE));
Then make a note of state wise ID's that the script returns.

Then use that in your script for comparison.

Why I suggest using console.log, rather than referring back to the configuration source is because, it works a bit differently.

Its explained below using a sample configuration source and a script that works with this source :

<state group="closed" icon="processattachment:/workflow/resolve.gif" id="s3" name="Done" showResolution="false">
   <action id="com.ibm.team.workitem.taskWorkflow.action.a1"/>
</state>
<state group="inprogress" icon="processattachment:/workflow/inprogress.gif" id="s2" name="In Progress" showResolution="false">
    <action id="com.ibm.team.workitem.taskWorkflow.action.stopWorking"/>
    <action id="com.ibm.team.workitem.taskWorkflow.action.resolve"/>
    <action id="com.ibm.team.workitem.taskWorkflow.action.a2"/>
</state>
<state group="closed" icon="processattachment:/workflow/reject.gif" id="com.ibm.team.workitem.taskWorkflow.state.s4" name="Invalid" showResolution="false"/>
<state group="open" icon="processattachment:/workflow/open.gif" id="s1" name="New" showResolution="false">
    <action id="com.ibm.team.workitem.taskWorkflow.action.startWorking"/>
    <action id="com.ibm.team.workitem.taskWorkflow.action.resolve"/>
    <action id="com.ibm.team.workitem.taskWorkflow.action.a2"/>
</state>


From this workflow configuration source, where ID's of different states are as follows :
Done         : s3
In Progress    : s2
Invalid         : com.ibm.team.workitem.taskWorkflow.state.s4
New         : s1

For this IDs, the below script works :
    /*******************************************************************************
     * Licensed Materials - Property of IBM
     * (c) Copyright IBM Corporation 2011. All Rights Reserved.
     *
     * Note to U.S. Government Users Restricted Rights: 
     * Use, duplication or disclosure restricted by GSA ADP Schedule
     * Contract with IBM Corp.
     *******************************************************************************/
    dojo.provide("ibm.custom.script.CompletionByStatus");
    dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");

    (function() {
        var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes;
        dojo.declare("ibm.custom.script.CompletionByStatus", null, {

            getValue: function(attribute, workItem, configuration) {
               
                var completionPercent = 0;
               
                console.log(workItem.getValue(WorkItemAttributes.STATE));
                   
                if(workItem.getValue(WorkItemAttributes.STATE) === "3")
                {    completionPercent = 10;    }
               
                else if(workItem.getValue(WorkItemAttributes.STATE) === "2")
                {    completionPercent = 20; }
                     
                else if(workItem.getValue(WorkItemAttributes.STATE) === "1")
                {    completionPercent = 40;}               
               
                else if(workItem.getValue(WorkItemAttributes.STATE) === "com.ibm.team.workitem.taskWorkflow.state.s4")
                {    completionPercent = 50;}
               
                else    {    completionPercent = 100;}
                     
                return completionPercent;
               
            }
        });
    })();

If you notice, the comparison is not with the direct id as it appears in the configuration source but against the trailing number on the id.
However, where the ID's are not of the form sx, it uses complete value of the id.

So, a console.log on the state to know the id returned in the script is going to be useful and essential in completing your script with correct condition values.

Hope this helps.


Comments
Donna Thomas commented Sep 25 '17, 11:08 a.m.

Just want to say Thank You! The only thing missing in the extremely detailed and correct response is how to view the console.log. I don't need it now, though.

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.