Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

Have an issue with Calculated scripts

Hi,


I have a script to automatically assign when the work item is in certain states as shown below. I have another field to capture previous state (CalcState). When I put the extra condition workItem.getValue(WorkItemAttributes.STATE) !== workItem.getValue("CalcState")  the script is not working. I take out the CalcState condition and the script works fine. Can you please take a look and see what I am doing wrong. 

dojo.provide("org.example.CalculatedBusinessOwner");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");

(function() {
var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes;
var CalculatedBusinessOwner= dojo.declare("org.example.CalculatedBusinessOwner", null, {
    getValue: function(attributeId, workItem, configuration) {
         if (workItem.getValue(WorkItemAttributes.TYPE) === "pcr" && workItem.getValue(WorkItemAttributes.STATE) === "com.ibm.team.workitem.pcrWorkflow.state.s13") { //Deployed to Prod
            return workItem.getValue("productionVerificationUsers");
        } else      
          if (workItem.getValue(WorkItemAttributes.TYPE) === "pcr" && workItem.getValue(WorkItemAttributes.STATE) === "com.ibm.team.workitem.pcrWorkflow.state.s4") && workItem.getValue(WorkItemAttributes.STATE) !== workItem.getValue("CalcState")  { // Development Complete 
            return workItem.getValue("itManager");
         } else 
          if (workItem.getValue(WorkItemAttributes.TYPE) === "pcr" && workItem.getValue(WorkItemAttributes.STATE) === "com.ibm.team.workitem.pcrWorkflow.state.s10") && workItem.getValue(WorkItemAttributes.STATE) !== workItem.getValue("CalcState") { // Approved 
            return workItem.getValue("itManager");
         } else {          
          if (workItem.getValue(WorkItemAttributes.TYPE) === "pcr" && workItem.getValue(WorkItemAttributes.STATE) === "com.ibm.team.workitem.pcrWorkflow.state.s5") && workItem.getValue(WorkItemAttributes.STATE) !== workItem.getValue("CalcState") { // Verified 
            return workItem.getValue("itManager");
         } else 
          if (workItem.getValue(WorkItemAttributes.TYPE) === "pcr" && workItem.getValue(WorkItemAttributes.STATE) === "com.ibm.team.workitem.pcrWorkflow.state.s14") && workItem.getValue(WorkItemAttributes.STATE) !== workItem.getValue("CalcState") { // Validated 
            return workItem.getValue("itManager");
         } else {         
            return workItem.getValue(WorkItemAttributes.OWNER);
        }
    }
});
})();

0 votes



2 answers

Permanent link

Some hints. 

Tidy up the script and simplify your logic. 
  1. Get the work item state into a variable. 
  2. Return the owner when the work item type is not PCR
Use Chrome to debug your script. The API is not made to be able to detect a state change and can also be called several times.

0 votes

Comments

Thanks Ralph.  I did tidy it up quite a bit. However, the previous state is always returning blank though the the previous state atttribute has a value. The previous state attribute is set using another calculated script. Can you tell me if this is possible at all.

Looking into my Chrystal Ball..... Comes up with nothing. You basically don't provide the information needed.  If workItem.getValue("CalcState") is always null, then the code that is supposed to set it to the attribute "CalcState" is interesting and not the code that reads it. Also which attribute type you use to store the value. I would suggest to debug that calculated value first to make sure that works.

 Ralph, the calculated state is working fine and is being set as shown below. Only the getvalue is returning empty...!MESSAGE LOG: 'Calc State : '



You still did not provide the type of that attribute. I can only guess it is string.

workItem.getValue("CalcState") likely returns null, because the attribute ID is not "CalcState".  You have to use the attribute ID e.g. if the ID is "the.id.of.the.attribute" use workItem.getValue("the.id.of.the.attribute") to retrieve the value. CalcState is the display name of the attribute and likely not the ID.

Look into the Work Item customization in the RTC Administration Web UI to find the attribute ID of the attribute. 

The attribute type is small string and yes I am using the id. Below is the code...


var witype=workItem.getValue(WorkItemAttributes.TYPE);
var wistate=workItem.getValue(WorkItemAttributes.STATE);
var prevstate=workItem.getValue("calcstate");
        if ((witype === "pcr") && (wistate === "com.ibm.team.workitem.pcrWorkflow.state.s4") && (prevstate !== "com.ibm.team.workitem.pcrWorkflow.state.s4")) {
            return workItem.getValue("itManager");
}
         } else          
            return workItem.getValue(WorkItemAttributes.OWNER); 

That code uses a different ID, if that ID is now the correct one, I assume your prevstate returns a value other than null. I also would suggest, again, to use Chrome and to debug your script. 

Thanks for all the help Ralph! I got this working by using a different logic. One last question. The custom attributes are hidden on the editor presentation for all states and on creation. The changes to these attributes are showing up in the history. Is there any way to suppress these entries? 

Suresh, you are changing a real attribute as a work around to be able to detect a state change. There is no way to configure an exception for one attribute to not participate in the audit history. So, no, you can't. A way that does not contaminate the work item history that much would be using a follow up action. In general I am not such a fan of automated ownership. You could basically have a query and/or a report that the IT Manager uses to see the items they should pick up. 

Thanks Ralph! Yes it is some people in my current client who are asking for these kind of requirements. 

As a consultant, it would be possible to point out when things should not be done and why. 8) But I know that there are those who don't want to listen and then also complain later when things start breaking. 

showing 5 of 10 show 5 more comments

Permanent link

 thank you Ralph Schoon for your help

0 votes

Your answer

Register or log in 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.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details

Question asked: Mar 18 '19, 3:16 p.m.

Question was seen: 1,740 times

Last updated: Mar 21 '19, 11:52 a.m.

Confirmation Cancel Confirm