It's all about the answers!

Ask a question

Why is my script-based calculated value causing the Filed Against value to reset to Unassigned every time I hit Save?


Michael Walker (99215201157) | asked Mar 26 '13, 1:10 a.m.
edited Mar 26 '13, 3:21 a.m. by Ralph Schoon (63.5k33646)
I'm trying to create a script-based calculated value that will set the Filed Against value when the Status is in a specific state. I can get this to work if I programatically set the Filed Against to a value in the script.

However, I don't want to do this in the final script so user's can change the Filed Against to
whatever they want in other states. The problem when I use the script below is everytime I
save a work item it changes the File Against value back to Unassigned, which is the default value.
This occurs even when I specifically set the Filed Against value to something else before hitting Save.

My goal is it will only automatically set the Filed Against value when in a specific state. Otherwise it stays what the user sets it to.

Dependencies are project area, status, and type.

Any ideas why my script below is causing the Filed Against to reset back to Unassigned?




dojo.provide("org.example.Awards_Test");

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

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

dojo.declare("org.example.Awards_Test", null, {

getValue: function(attributeId, workItem, configuration) {

var unassigned = "_mlNToHhdEeK0VvmfhffD_w";

var swg = "_mv6u1HhdEeK0VvmfhffD_w";

var swgInfo = "_WcavgHh7EeKkT71PvOj-dQ";

var swgMDM = "_XTrusHh7EeKkT71PvOj-dQ";


//var filed = workItem.getValue(WorkItemAttributes.FILED_AGAINST);
//console.log("filed: " + filed);

var state = workItem.getValue(WorkItemAttributes.STATE);
console.log("state: " + state);

var newFiled;


if (workItem.getValue(WorkItemAttributes.TYPE) === "com.ibm.team.apt.workItemType.epic" && workItem.getValue(WorkItemAttributes.STATE) === "awared.state.s3") {

if (workItem.getValue(WorkItemAttributes.FILED_AGAINST) === swg) {
console.log("swg");
newFiled = swgInfo;
return newFiled ;
}

else if (workItem.getValue(WorkItemAttributes.FILED_AGAINST) === swgInfo) {
console.log("swgInfo");
newFiled = swgMDM;
return newFiled ;
}


else {
return Status.OK_STATUS;
}




}
}});
})();

Accepted answer


permanent link
Ralph Schoon (63.5k33646) | answered Mar 27 '13, 5:46 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
This script worked for me at least in the tests I did in 4.0.1. It triggers against Type, State and Filed Against as dependencies.

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


(function() {
	var doDebug = true;
	var scriptname = "CategoryAttributeValueAnalyzer";
	var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;

dojo.declare("com.acme.providers.script.CategoryAttributeValueAnalyzer", null, {

    getValue: function(attributeId, workItem, configuration) {
    	debug("- Start"); 

 		var type = workItem.getValue(WorkItemAttributes.TYPE);
		debug("Type: " + type);
		var category = workItem.getValue(WorkItemAttributes.FILED_AGAINST);
		debug("Filed Against: " + category);
		var state = workItem.getValue(WorkItemAttributes.STATE);
		debug("State: " + state);
		
		if(state=="3"){
			if(type=="defect"){
				return "_NOjVq3T1EeK_I-am-aV--w";
			}
		}

		return category;
		
    	function debug(display){	    		
    		if(doDebug){
        		console.log(scriptname + " " + display);
    		}
    	}
    }
});
})();
Michael Walker selected this answer as the correct answer

Comments
Michael Walker commented Mar 28 '13, 1:27 a.m.

Thank you for the information Ralph.  We'll be using a work item template to set the Filed Against initially along with other fields.  My script seems to work and correctly set the Filed Against value based on the State. 

I also implemented a script against State so when the Filed Against and State reach certain values, it will change both the Filed Against and State during same Save action.

The team we're supporting wants to track Awards through RTC and have a state called Promoted that will move the Award up a level and reset the workflow to Submitted.

4 other answers



permanent link
Ralph Schoon (63.5k33646) | answered Mar 26 '13, 3:29 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Michael,

one thing that is very disturbing in the code above is the line return Status.OK_STATUS;
This line does not make sense at all. A calculated value provider always returns the value of the calculation. If the value should not change, it returns the old value.

Status if for validations or conditions. Another thing to mention is that calculated attributes should typically be read only. More examples and background can be found here: https://jazz.net/library/article/1093 Lab 5.

Without knowing the background, the filed against attribute is something that I would typically not touch myself in an automation. 

Comments
Michael Walker commented Mar 26 '13, 2:52 p.m.

Ralph,

The line return Status.OK_STATUS; I added in during my latest test before posting because previous attempts were failing to return what I desired.  I had used this line in previous scripts when dealing with changing Owners based on specific States and wanted to try it to see if it would prevent the Filed Against from resetting. I knew it was a long shot and understand it shouldn't be there.

What I'm noticing is Filed Against behaves differently from the other fields like Status and Owner.  When I set a variable to workItem.getValue(WorkItemAttributes.FILED_AGAINST); and then return the variable at the end it's returning the previous Filed Against value.

For example, user modifies a Work Item and changes Filed Against from "Unassigned" to "Team A".  When I hit Save and the script fires the variable is set to "Unassigned" and thus reverts the Filed Against value from "Team A"


Michael Walker commented Mar 26 '13, 2:54 p.m.

(continued)   back to "Unassigned".  With the Status and Owner fields it would populate the variables with the new values at Save.  It only works if I hardcode the variable in the script with one of the Filed Against values.

If I could get it to store the new value for Filed Against my issue would be solved and I could have it modify the Filed Against when a certain Status is achieved.


permanent link
Abuzaid Shaikh (89146) | answered Mar 26 '13, 3:32 a.m.
 Hi Michael,


Instead of just declaring the variable newFiled  without any value.

You can set:

var newFiled = workItem.getValue(WorkItemAttributes.FILED_AGAINST);

and in the last line you can just return newFiled.

else { 
return 
newFiled 


Hope this helps you.

Comments
Michael Walker commented Mar 26 '13, 2:58 p.m.

Abuzaid,

I had tried that initially but it returns the old Filed Against value and not the new one.

For example, when the script is enabled if I access a Work Item and change the File Against from "Unassigned" to "Team A" and then hit Save the variable is populated with "Unassigned" rather than the new value "Team A".

This is different than with other fields like Status and Owner.  With those if I do :
var state = workItem.getValue(WorkItemAttributes.STATE);
console.log("state: " + state);

and in the Work Item I change the State from Open to Working the variable will be populated with Working in the script after I hit Save.  I need it to store the new value.


Abuzaid Shaikh commented Mar 27 '13, 4:07 a.m.

Hi Michael,


Can you use 
var filed = workItem.getValue("com.ibm.team.workitem.attribute.category"); 
console.log("Filed: "+ filed);

and see what value it returns when you change the value.




permanent link
sam detweiler (12.5k6195201) | answered Mar 26 '13, 5:26 p.m.
edited Mar 27 '13, 7:40 a.m.
looks like a bug in the script processor passing the oldstate filed_against, instead of the newstate value

Comments
Michael Walker commented Mar 26 '13, 5:48 p.m.

I tried a simple script that would show the variable values in the eclipse log.  After opening a work item and saving it, I went back in and changed the Filed Against value and the Status value.  The log showed the Status variable changed to the new value, but the Filed Against value kept the old value. 

Currently it would require me to hardcode a Filed Against value in the variable to get it to work.



var state = workItem.getValue(WorkItemAttributes.STATE);
     
    var type = workItem.getValue(WorkItemAttributes.TYPE);
     
     
    var filed = workItem.getValue(WorkItemAttributes.FILED_AGAINST);

     
    console.log("filed: " + filed);
    console.log("Type: " + type);
    console.log("State: " + state);


permanent link
Ralph Schoon (63.5k33646) | answered Mar 27 '13, 4:41 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
edited Mar 27 '13, 4:50 a.m.
I used the script below as calculated value (for description as described in the Process Enactment Workshop). It triggers against Type, State and Filed Against as dependencies. And it seems to be working for me. At least it shows the changed category as soon as I change it. Please be aware that the state change becomes not active immediately, other than the category change. The state becomes active after the save was processed.

This was on RTC 4.0.1.


/*******************************************************************************
 * 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("com.acme.providers.script.AttributeValueAnalyzer");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("dojo.date");
dojo.require("dojo.date.stamp");


(function() {
	var doDebug = true;
	var scriptname = "AttributeValueAnalyzer";
	var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;

dojo.declare("com.acme.providers.script.AttributeValueAnalyzer", null, {

    getValue: function(attributeId, workItem, configuration) {
    	debug("- Start"); 

    	var out = "Attribute Values:\n";
		
		out+=getAttributeData("ID: ", WorkItemAttributes.ID);
		out+=getAttributeData("TYPE: ", WorkItemAttributes.TYPE);
		out+=getAttributeData("Project Area: ", WorkItemAttributes.PROJECT_AREA);
		out+=getAttributeData("Filed Against: ", WorkItemAttributes.FILED_AGAINST);
		out+=getAttributeData("State: ", WorkItemAttributes.STATE);


		out+="\nCustom:\n";

		return out;
		
    	function debug(display){	    		
    		if(doDebug){
        		console.log(scriptname + " " + display);
    		}
    	}
    	
    	function getAttributeData (message,attributeID){
    		debug("Get Attribute Data for " + attributeID);
    		var isSet="";
    		var attributeValue="";
    		var attributeLabel="";
    		var result = message + "\n";
    		
    		try{
    			isSet=workItem.isAttributeSet(attributeID);
    			attributeValue=workItem.getValue(attributeID);
    		} catch (e) {
    			attributeValue = "Exception reading the attribute value";
			}
    		try{
    			attributeLabel=workItem.getLabel(attributeID);
    		} catch (e) {
    			attributeLabel= "Exception reading the attribute label:" + e.toLocaleString();
			}
    		result +="Set: " + isSet + "\nValue= " + attributeValue + "\nLabel= " + attributeLabel;
    		debug(result);
    		return result + "\n\n";
    	}
    }
});
})();

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.