Basic update of custom field issue
I have seen that in Gantt charts you need a Start Date for each task and if you have time tracking enabled one of your selections is to user Timer Time. If i understood well though this is not correct cause Timer time can be updated every time someone start Timer and stops it. So this is not the correct selection and for this reason i have created a custom field in the project named Start Date.
From this i wanted to have a workflow and on change of an issue from Open state to In Progress state and if the custom field is not already assigned to Update it with new Date().
SO on transition
I get error that the Start Date field can not be updated.
It seems to me that probably there is an issue with the requirements of the workflow on the code. I really do not know what needs to be exactly there and how and i try to go from examples. I understand that you have to place all dynamic variables that you are using in workflow to be sure that these fields exists on the project but i am not sure exactly on the details.
Also if i remove the requirement though the value is assigned the field in the issue is not updated.
The workflow is the next:
/**
* This is a template for an on-change rule. This rule defines what
* happens when a change is applied to an issue.
*
* For details, read the Quick Start Guide:
* https://www.jetbrains.com/help/youtrack/server/2023.1/Quick-Start-Guide-Workflows-JS.html
*/
const entities = require('@jetbrains/youtrack-scripting-api/entities');
const workflow = require('@jetbrains/youtrack-scripting-api/workflow');
exports.rule = entities.Issue.onChange({
// TODO: give the rule a human-readable title
title: 'Issue_start_time',
guard: (ctx) => {
// TODO specify the conditions for executing the rule
const issue = ctx.issue;
workflow.message(issue.fields.StartDate);
if(!issue.StartDate){
workflow.message("before update");
workflow.message(issue.fields.StartDate);
return issue.was('State', 'Open') && issue.becomes('State', "In Progress");
}
return false;
},
action: (ctx) => {
const issue = ctx.issue;
workflow.message('Update should happen');
issue.fields.StartDate=new Date();
// TODO: specify what to do when a change is applied to an issue
workflow.message("After update");
workflow.message(issue.fields.StartDate);
},
requirements: {
State: {
type: entities.State.fieldType,
'Open': {},
'In Progress': {}
},
StartDate: {
type: entities.Field.dateType,
name: 'Start Date'
}
// TODO: add requirements
}
});
Also if we comment the last lines where in requirements we declare the StartDate field that is needed. Then
from the workflow.messages we get
The first 3 messages from the bottom are coming from the guard. "Update should happen" is in action and then after the update you see that the variable has value. But in the issue only the state changes and the "Start Date" is still empty
Is there any other better way to debug ? cause with console log you have to switch tabs to do the state transition and then switch to the workflow edit to update the console to see the log.
Please sign in to leave a comment.
Hello,
I'm Lena from the YouTrack Team.
Could you please try to change the Start Date field type to `date and time` and in the requirements use the following code:
Please let me know your results.
I did this change also in field setup and in requirements and it did not work also. I have checked also another build in workflow that updates due date and it seems the same or I am missing something
I did this change also in field setup and in requirements and it did not work also. I have checked also another build in workflow that updates due date and it seems the same or I am missing something
Though you have example of setting the Due Date variable using just :
issue.fields.DueDate = new Date()
This does not work. At some time i took an error that the value should be passed as instant and not as date.
i succedd to update the value using:
issue.fields.StartDate = Date.now()
which returns the miliseconds of a date and it worked . Is this a version issue or not?
Hello,
I'm happy to hear that you found a solution.
I'm not aware of any changes. Could you please preform a small test for me: log in console (or using workflow messages) `new Date()` and `Date.now()`?
the first is date in string format readable the other is long number like 123465565L
Thank you for sharing the results. It looks like the `Date.now()` is the correct option until now. We will update our public resources according to this fact. Thank you for your contribution.