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
      },
   }
});

12 comments
Comment actions Permalink
Official 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:

subtask.fields["Sistema"] = issue.fields.Sistema;

or

subtask.fields[ctx.Sistema.name] = issue.fields.Sistema;

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.

 

 

Comment actions Permalink

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:

 

var entities = require('@jetbrains/youtrack-scripting-api/entities');
exports.rule = entities.Issue.onChange({
title: 'Propagate Values',
guard: function (ctx) {
return ctx.issue.links['parent for'].added.isNotEmpty() &&
ctx.issue.fields.Fecha;
},
action: function (ctx) {
var issue = ctx.issue;
var safeSetFecha = function (subtask) {
if (subtask.project && !subtask.project.isArchived) {
if (subtask.project.key === issue.project.key ||
subtask.project.findFieldByName(ctx.Fecha.name)) {
if (!subtask.fields[ctx.Fecha.name]) {
if (!issue.fields.Fecha ||
subtask.project.findFieldByName(ctx.Fecha.name).findValueByName(issue.fields.Fecha.name)) {
subtask.fields[ctx.Fecha.name] = issue.fields.Fecha;
}
}
}
}
};

issue.links['parent for'].added.forEach(safeSetFecha);
},
requirements: {
Fecha: {
type: entities.Field.dateType,
name: 'Fecha planificacion'
}
},
SubtaskOf: {
type: entities.IssueLinkPrototype,
name: 'Subtask',
outward: 'parent for',
inward: 'subtask of'
}
});

 

0
Comment actions Permalink

Would you please provide me with the error shown in the workflow editor console

0
Comment actions Permalink

Yes, Oleg Larshin.

Thanks for your time.

 

Processing issue P000_PMPClone-10:
TypeError: Cannot find function findValueByName in object {"becomesRemoved": "false",
"isNew": "false",
"$$type": "SimpleProjectCustomField",
"toString": function() {},
"toShortString": function() {},
"required": function() {},
"becomes": function() {},
"isChanged": function() {},
"oldValue": function() {},
"canBeReadBy": function() {},
"canBeWrittenBy": function() {},
"constructor": function() {},
"becomesInvisibleInIssue": function() {},
"becomesVisibleInIssue": function() {},
"getBackgroundColor": function() {},
"getForegroundColor": function() {},
"localizedName": "null",
"name": "Fecha planificacion",
"getValuePresentation": function() {},
"isVisibleInIssue": function() {},
"nullValueText": "No hay fecha planificacion"}. (pruebas/campofecha#15)
 
            org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4198)
            org.mozilla.javascript.gen.pruebas_campofecha_4346._c_anonymous_3(pruebas/campofecha:15)
            org.mozilla.javascript.gen.pruebas_campofecha_4346._c_anonymous_2(pruebas/campofecha:24)
0
Comment actions Permalink

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. 

0
Comment actions Permalink

Okey Thanks, It works without findValueByName method.

Thanks a lot.

0
Comment actions Permalink

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.

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.Entorno;
},
action: function (ctx) {
var issue = ctx.issue;
var safeSetEntorno = function (subtask) {
if (subtask.project && !subtask.project.isArchived) {
if (subtask.project.key === issue.project.key ||
subtask.project.findFieldByName(ctx.Entorno.name)) {
if (!subtask.fields[ctx.Entorno.name]) {
if (!issue.fields.Entorno ||
subtask.project.findFieldByName(ctx.Entorno.name)) {
subtask.fields[ctx.Entorno.name] = issue.fields.Entorno;
}
}
}
}
};
issue.links['parent for'].added.forEach(safeSetEntorno);
},
requirements: {
Entorno: {
type: entities.EnumField.fieldType,
multi: true
},
SubtaskOf: {
type: entities.IssueLinkPrototype,
name: 'Subtask',
outward: 'parent for',
inward: 'subtask of'
}
}
});
0
Comment actions Permalink

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. 

 

0
Comment actions Permalink

Hello,

Can you show me an example of how to use it? please.

Thanks.

0
Comment actions Permalink

It should be something like:

issue.fields.Entorno.forEach(function(value){
subtask.fields[ctx.Entorno.name].add(value);
});
0
Comment actions Permalink

Thank you very much, it already works, thanks for your time.

0
Comment actions Permalink

You are always welcome! Should you have any further questions, feel free to contact us at any time. Have a great day! 

0

Please sign in to leave a comment.