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

How to set value of field to current timestamp based on particular state?

 I have 2 medium string fields "Current State" and "Date".

When the state of a workitem is NEW, it should set the value in "Current State" to NEW and update the current date in field "Date".

I tried to reuse the below script, but its not working. There is no error, but no value is returned. 
Can you suggest, what is wrong with this script? OR what is the correct way to achieve this?
_____
dojo.provide("client.valueProvider.currentInProgressDate"); 
dojo.require("dojo.date.stamp"); 
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes"); 

(function() { 
    var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes; 
    dojo.declare("client.valueProvider.currentInProgressDate", null, { 
        getValue: function(attribute, workItem, configuration) { 
            var now = dojo.date.stamp.toISOString(new Date(), {milliseconds:true, zulu:true}); 
            var dayZero = dojo.date.stamp.toISOString(new Date(0), {milliseconds:true, zulu:true}); 
            var state = workItem.getValue(WorkItemAttributes.STATE); 
            var myOldValue = workItem.getValue(attribute); 
            if (state) {            
                switch (state) { 
                    case 'scr.workflow.state.s7':                //Project Director Defer
                        return now; 
                        break;                 
                    case 'scr.workflow.state.s1':                //New 
                        if (myOldValue != null) { 
                            return dayZero; 
                        } 
                        break;                 
                } 
            } 
            return myOldValue; 
        } 
    }); 
}) (); 
_____

Please suggest.

0 votes



2 answers

Permanent link

 User 

var CurrentDate= new Date();
for current date and return the same

1 vote


Permanent link

How would you trigger the script? What dependency? The State? This will likely not work, since the state changes after the save so the script would not see the change, I think. If you search this forum, you will likely realize that this has been discussed many, many times already. Some users have found ways around the problem, by using a hidden "Old State" attribute.  Another solution is a follow up action (Java Server Extension).

0 votes

Comments

 Hi Ralph, can you refer me to the logic of hidden state attribute logic/url? 


Earlier, I have 2 methods in mind:
#1: the "Current State" field should get value of selected state and "Date" should get system date, as soon as a workitem state is selected.
But the problem here is, I am not finding a way to invoke script based on state.
#2: the "Current State" field should get value of state and "Date" should get system date, on save. 
But the problem here is, I am not finding a way to invoke script on save.

I have been browsing though jazz.net but didnt find sample script for any of these methods which can be a starting point for me.

Please suggest.

You need to think about it carefully. If the NEW state is the initial state in the work flow, I don't see any way you can trigger the calculated value script, that is during the submission. If it is indeed the initial state, you should consider using a default value, and hide the attribute in the editor. Also, if the work item is modified with the same state, should the "Date" be updated? If you want to make it complicated, the follow-up participant may be your only option.

thanks for catching the "Initial state" point. 

I think having the "Current State" field updated with current state, and date field with current time, on "Save", sounds a good approach.
How to trigger a calculate script "on Save"? can you share a script, that will be a good starting point for me and whoever is looking for such stuff.

It will not work for any changes during the save operation due to the limitation of JavaScript. I was not proposing how it could work, but rather, how it may not work.

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
× 12,019

Question asked: Jul 13 '17, 8:55 p.m.

Question was seen: 2,388 times

Last updated: Jul 18 '17, 6:00 a.m.

Confirmation Cancel Confirm