It's all about the answers!

Ask a question

Script based Default Value


Cesar Sasaki (511166102) | asked Jul 03 '17, 7:10 p.m.

 Hi


I'm trying to create a new "Script Based Default Value" customization. It is simple just to hard code the owner of a certain type of workitem. I have created  calculated value scripts before and they run ok.
But I'm having trouble with the default value, is the structure the same? 
This is my code :

dojo.provide("Default_Value.task");
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("Default_Value.task", null, {

    getValue: function(attributeId, workItem, configuration) {
var wi_type = workItem.getValue(WorkItemAttributes.TYPE);
var wi_owner = workItem.getValue(WorkItemAttributes.OWNER);
console.log("Type - " + wi_type);
console.log("Owner - " + wi_owner);
if (wi_type == "task"){
wi_owner  = "_p_g14CR6EeWmoeAO1lg85w"; 
}
    return wi_owner;
    }
});
})(); 

Thanks

Accepted answer


permanent link
Donald Nong (14.3k213) | answered Jul 03 '17, 11:27 p.m.

The function should be getDefaultValue, not getValue. You can use the "Fill in example" link to get the skeleton of the script to avoid such simple mistake.
https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Script_based_default_values

Cesar Sasaki selected this answer as the correct answer

Comments
Cesar Sasaki commented Jul 04 '17, 2:05 a.m.

 Hi Donald


I'm still having trouble using this code :
getDefaultValue: function(attribute, workItem, configuration) {
        var wi_type       = workItem.getDefaultValue(WorkItemAttributes.TYPE);
var wi_owner = workItem.getDefaultValue(WorkItemAttributes.OWNER);

if (wi_type == "task"){
wi_owner  = "_p_g14CR6EeWmoeAO1lg85w"; 
}
    return wi_owner;


Donald Nong commented Jul 04 '17, 3:08 a.m.

More details please?

One scenario where it definitely will not work is creation. The default value provider will be executed on the server side before the editor is rendered on the web UI. At that time, all attributes have no value, so your script will always return null. You can add console.log() to debug and confirm this.


Ralph Schoon commented Jul 04 '17, 4:26 a.m. | edited Jul 04 '17, 4:26 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

 The code above is bogus I suggest to read the instructions again https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Script_based_default_values 


workItem.getDefaultValue(WorkItemAttributes.TYPE);
must be workItem.getValue(WorkItemAttributes.TYPE);

the function was wrong as Donald correctly stated. Also Default values have very limited value since there is almost no data available on creation of the work item as Don also stated.


Cesar Sasaki commented Jul 04 '17, 11:30 a.m.

 I changed my script and worked I needed to add this :

(function() {
dojo.declare("default_value.owner", null, {

    getDefaultValue: function(attribute, workItem, configuration) {
    var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes; 
 
    var wi_type = workItem.getValue(WorkItemAttributes.TYPE);

Your answer


Register or to post your answer.