Calculated Values - Setting current date
I have 3 Timestamp fields that needs to set to current date based on the 3 values (1,2,3) selection in a enumerated field.
For example-
When i select 1 the first date field should be set to current date.
When i select 2 the second date field should be set to current date (Without losing the value for first field)
When i select 3 the third date field should be set to current date. (Without losing the value for first or second field)
Now the problem is when i select 1 the first date field gets set to current date which is as expected. When i select 2 the second date field gets set to current date but here I also lose the value for the first date field which was set when i selected 1. same thing happens when i select 3.
Any advice on how can achieve this without losing any values ?
Below is the script i am using-
dojo.provide("com.example.script.CurrDateForOne");
dojo.require("dojo.date"); // We need the date class from Dojo to compare two dates
dojo.require("dojo.date.stamp"); // We need the stamp class to work with ISO date strings
(function() {
dojo.declare("com.example.script.CurrDateForOne", null, {
getValue: function(attribute, workItem, configuration) {
//get the current date
var date=new Date();
var currDate=dojo.date.stamp.toISOString(date, {milliseconds:true, zulu:true});
console.log("Date: "+currDate);
//get the current status
var status=workItem.getValue("com.example.team.workitem.fdreviewStatus");
console.log("Status: "+status);
if(status === "com.example.team.workitem.enum.ReviewStatus.literal.l3"){
return currDate ;
}
}
});
})();
For example-
When i select 1 the first date field should be set to current date.
When i select 2 the second date field should be set to current date (Without losing the value for first field)
When i select 3 the third date field should be set to current date. (Without losing the value for first or second field)
Now the problem is when i select 1 the first date field gets set to current date which is as expected. When i select 2 the second date field gets set to current date but here I also lose the value for the first date field which was set when i selected 1. same thing happens when i select 3.
Any advice on how can achieve this without losing any values ?
Below is the script i am using-
dojo.provide("com.example.script.CurrDateForOne");
dojo.require("dojo.date"); // We need the date class from Dojo to compare two dates
dojo.require("dojo.date.stamp"); // We need the stamp class to work with ISO date strings
(function() {
dojo.declare("com.example.script.CurrDateForOne", null, {
getValue: function(attribute, workItem, configuration) {
//get the current date
var date=new Date();
var currDate=dojo.date.stamp.toISOString(date, {milliseconds:true, zulu:true});
console.log("Date: "+currDate);
//get the current status
var status=workItem.getValue("com.example.team.workitem.fdreviewStatus");
console.log("Status: "+status);
if(status === "com.example.team.workitem.enum.ReviewStatus.literal.l3"){
return currDate ;
}
}
});
})();
Accepted answer
HI Akshay,
I modified the conditions a bit and it seems to be working, check the below revised code :
dojo.provide("com.example.script.CurrDateForOne");
dojo.require("dojo.date"); // We need the date class from Dojo to compare two dates
dojo.require("dojo.date.stamp"); // We need the stamp class to work with ISO date strings
(function()
{
dojo.declare("com.example.script.CurrDateForOne", null,
{
getValue: function(attribute, workItem, configuration)
{
var date=new Date();
var currDate=dojo.date.stamp.toISOString(date, {milliseconds:true, zulu:true});
var status=workItem.getValue("com.example.team.workitem.fdreviewstatus");
if (attribute === "stat1date" && status === "reviewStatusEnum.literal.l2")
{
return currDate;
}
else
{
return workItem.getValue("stat1date");
}
}
});
})();
Guess you plan to have a total of three scripts, each watching over their attribute values in the condition internally.
I tried to get one script for all three attributes but it seemed to be tricky.. three separate scripts do the trick easily instead...
hope this helps.
I modified the conditions a bit and it seems to be working, check the below revised code :
dojo.provide("com.example.script.CurrDateForOne");
dojo.require("dojo.date"); // We need the date class from Dojo to compare two dates
dojo.require("dojo.date.stamp"); // We need the stamp class to work with ISO date strings
(function()
{
dojo.declare("com.example.script.CurrDateForOne", null,
{
getValue: function(attribute, workItem, configuration)
{
var date=new Date();
var currDate=dojo.date.stamp.toISOString(date, {milliseconds:true, zulu:true});
var status=workItem.getValue("com.example.team.workitem.fdreviewstatus");
if (attribute === "stat1date" && status === "reviewStatusEnum.literal.l2")
{
return currDate;
}
else
{
return workItem.getValue("stat1date");
}
}
});
})();
Guess you plan to have a total of three scripts, each watching over their attribute values in the condition internally.
I tried to get one script for all three attributes but it seemed to be tricky.. three separate scripts do the trick easily instead...
hope this helps.
One other answer
Hi Dinesh
Thank you for the reply. Yes i tried with above script it works perfectly fine!!
Another way to do the same would be as follows-
/*******************************************************************************/
dojo.provide("com.example.script.CurrDateForApprovedDPL");
dojo.require("dojo.date"); // We need the date class from Dojo to compare two dates
dojo.require("dojo.date.stamp"); // We need the stamp class to work with ISO date strings
(function() {
dojo.declare("com.example.script.CurrDateForApprovedDPL", null, {
getValue: function(attribute, workItem, configuration) {
console.log("CurrDateForApprovedDPL");
var resultDate= null;
//Current value set in date field
var currDateValue=dojo.date.stamp.fromISOString(workItem.getValue(attribute));
console.log("Current value set is : "+currDateValue);
//get the current date
var date=new Date();
var currDate=dojo.date.stamp.toISOString(date, {milliseconds:true, zulu:true});
//get the current status
var status=workItem.getValue("com.example.team.workitem.dplreviewstatus");
console.log("Status: "+status);
if(currDateValue != null){
resultDate=currDateValue;
}
else{
if(status === "com.example.team.workitem.enum.ReviewStatus.literal.l5"){
resultDate=currDate;
}
}
return resultDate;
}
});
})();
Thank you for the reply. Yes i tried with above script it works perfectly fine!!
Another way to do the same would be as follows-
/*******************************************************************************/
dojo.provide("com.example.script.CurrDateForApprovedDPL");
dojo.require("dojo.date"); // We need the date class from Dojo to compare two dates
dojo.require("dojo.date.stamp"); // We need the stamp class to work with ISO date strings
(function() {
dojo.declare("com.example.script.CurrDateForApprovedDPL", null, {
getValue: function(attribute, workItem, configuration) {
console.log("CurrDateForApprovedDPL");
var resultDate= null;
//Current value set in date field
var currDateValue=dojo.date.stamp.fromISOString(workItem.getValue(attribute));
console.log("Current value set is : "+currDateValue);
//get the current date
var date=new Date();
var currDate=dojo.date.stamp.toISOString(date, {milliseconds:true, zulu:true});
//get the current status
var status=workItem.getValue("com.example.team.workitem.dplreviewstatus");
console.log("Status: "+status);
if(currDateValue != null){
resultDate=currDateValue;
}
else{
if(status === "com.example.team.workitem.enum.ReviewStatus.literal.l5"){
resultDate=currDate;
}
}
return resultDate;
}
});
})();