Propagate values between issues
Hellow, I have done an script to propagate values between issues but the problem is that only works for enum values, when I change the value type at requeriments it doesnt work, how can I make it work for Groups, Dates and multi enum values?.
Thanks.
Heare is the script:
var entities = require('@jetbrains/youtrack-scripting-api/entities');
exports.rule = entities.Issue.onChange({
title: 'Heredar campos',
guard: function (ctx) {
return ctx.issue.links['parent for'].added.isNotEmpty() && ctx.issue.fields.Sistema &&;
},
action: function (ctx) {
var issue = ctx.issue;
var safeSetSistema = function (subtask) {
if (subtask.project && !subtask.project.isArchived) {
if (subtask.project.key === issue.project.key ||
subtask.project.findFieldByName(ctx.Sistema.name)) {
if (!subtask.fields.Sistema) {
if (!issue.fields.Sistema ||
subtask.project.findFieldByName(ctx.Sistema.name).findValueByName(issue.fields.Sistema.name)) {
subtask.fields.Sistema = issue.fields.Sistema;
}
}
}
}
};
issue.links['parent for'].added.forEach(safeSetSistema);
},
requirements: {
Sistema: {
type: entities.EnumField.fieldType
},
}
});
Please sign in to leave a comment.
Hello,
The issue is that those attributes that you declare in requirements (like Sistema) gets represented as issue.fields[key] only for the issue accessible via ctx.issue. So to access the field in a newly created issue you'll need to refer to it as:
or
We are planning to update our documentation in the context of the Add a note to the workflow documentation that describes that aliases can be used only for ctx.issue issue.
Should you have any further questions, feel free to contact us.
Thanks for your answer, I try it and it works for enum fields but when I change to other type of field it doesnt work, I try it with a date field and I have this issue code: Issue: id = 2-6165
This is the code:
Would you please provide me with the error shown in the workflow editor console?
Yes, Oleg Larshin.
Thanks for your time.
Thanks! Now I see the issue. You are right, only enumerable field types contain a set of values. For other types (like date, int, string, etc.) there is no such a set. As a possible solution, I suggest you check the field type before calling the findValueByName method. I hope it will be helpful.
Okey Thanks, It works without findValueByName method.
Thanks a lot.
Hello Oleg Larshin,
I have an other problem with multi enum values, is the same code but it doesnt work, could you help me?
Now I dont have any error but it doesnt work.
Tanks.
Hello,
When a custom field is multiple, it contains a set of values. So, to copy values to another field, it is necessary to iterate through values using the forEach method and add them with the add method. Please refer to the Working with Set Objects for more information.
Hello,
Can you show me an example of how to use it? please.
Thanks.
It should be something like:
issue.fields.Entorno.forEach(function(value){
subtask.fields[ctx.Entorno.name].add(value);
});
Thank you very much, it already works, thanks for your time.
You are always welcome! Should you have any further questions, feel free to contact us at any time. Have a great day!
For anyone who might stumble upon this thread and try using the latest WF code posted by Alvmonroy here, you might need to change this line:
To this:
That's because in here, you need to check whether the value in the custom field (
Entorno
) of the subtask corresponds to the current (parent) issue. Hope that helps!Hi,
I'm trying to copy values to draft (as described here) but my code does not work:
Error: “Cannot set value to custom field DC”
What am I doing wrong?
Thanks.
Hi,
If the field uses different Bundle, the correct (working) code looks like this:
It turns out that the add() method can accept not only an object, as described in the documentation, but also a string. The only catch is that this code will throw an error if a value with that name does not exist in the target Set.
Hello Viacheslav,
We are happy to hear that it works for you. I checked your original code in the testing environment, and it works fine. Are both issues in the same project? If you wish a detailed investigation, we will require some additional private materials. For this, please create a ticket in our support centre: https://jb.gg/ytsup
Thank you.