It's all about the answers!

Ask a question

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

Pravin Patil (104139133) | asked Jul 13 '17, 8:55 p.m.

 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?

(function() { 
    var WorkItemAttributes =; 
    dojo.declare("client.valueProvider.currentInProgressDate", null, { 
        getValue: function(attribute, workItem, configuration) { 
            var now = Date(), {milliseconds:true, zulu:true}); 
            var dayZero = 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; 
                    case 'scr.workflow.state.s1':                //New 
                        if (myOldValue != null) { 
                            return dayZero; 
            return myOldValue; 
}) (); 

Please suggest.

2 answers

permanent link
Ralph Schoon (63.2k33646) | answered Jul 14 '17, 7:02 a.m.

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).

Pravin Patil commented Jul 14 '17, 12:15 p.m.

 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 but didnt find sample script for any of these methods which can be a starting point for me.

Please suggest.

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

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.

Pravin Patil commented Jul 17 '17, 5:24 p.m.

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.

Donald Nong commented Jul 18 '17, 6:00 a.m.

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.

permanent link
Pankaj Sharma (401170) | answered Jul 14 '17, 12:36 p.m.


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

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.