Set field value on other project

Hello, 

I've created a workflow to copy issue and move it to another project. In general it works fine, but... Here is my case:

 

There is:

* Project A with field Client version;

* Project B with field Affected version.

Both fields shares one set of values (merged), so when I create a copy of issue of Project A and move it to Project B, I want Affected version value to be the same as Project A Client version value.

Is there any posibility to do that? Because simple workflow assignment (newissueID.fields.Affected = issue.fields.Clversion) doesn't work. Here is my wokflow code:

var entities = require('@jetbrains/youtrack-scripting-api/entities');
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');

exports.rule = entities.Issue.action({
// TODO: give the rule a human-readable title
title: 'Clone +',
// TODO: define the custom command
command: 'clone_2',
guard: function(ctx) {
return ctx.issue.isReported;
},
action: function(ctx) {
var issue = ctx.issue;
var newIssue = ctx.issue.copy();
var newissueID = entities.Issue.findById(newIssue.id);

issue.fields.Department = newissueID.fields.Department;
issue.fields.Unit = newissueID.fields.Unit;
issue.fields.Product = newissueID.fields.Product;
issue.fields.Type = newissueID.fields.Type;
issue.fields.Status = newissueID.fields.Status;
issue.fields.Priority = newissueID.fields.Priority;
issue.fields.Client = newissueID.fields.Client;
issue.fields.Assignee = newissueID.fields.Assignee;
issue.fields.Sprint = newissueID.fields.Sprint;
issue.fields.Price = newissueID.fields.Price;
newissueID.summary = 'Clone ' + issue.id + ' ' + issue.summary;
newissueID.description = null;

var projectB = entities.Project.findByName('ProjectB');
newissueID.project = projectB;
newissueID.links[ctx.Clone.inward].add(issue);

},
requirements: {
Department:{
type: entities.EnumField.fieldType,
name: 'Department'
},
Unit:{
type: entities.EnumField.fieldType,
name: 'Unit'
},
Type:{
type: entities.EnumField.fieldType,
name: 'Type'
},
Priority:{
type: entities.EnumField.fieldType,
name: 'Priority'
},
Client:{
type: entities.EnumField.fieldType,
name: 'Client'
},
Assignee:{
type: entities.User.fieldType,
name: 'Assignee'
},
Sprint:{
type: entities.ProjectVersion.fieldType,
name:'Sprint'
},
Clversion:{
type: entities.ProjectVersion.fieldType,
name: 'Client TP version'
},
Affected:{
type: entities.ProjectVersion.fieldType,
name: 'Affected version'
},
Price:{
type: entities.EnumField.fieldType,
name: 'Price'
},
Clone: {
type: entities.IssueLinkPrototype,
outward: "cloned to",
inward: "clone of"
},
StoryPoints:{
type: entities.Field.integerType,
name: 'Story points'
},
Product:{
type: entities.EnumField.fieldType,
name: 'Product'
},
}
});

 

Thank you in advance.

 

11 comments
Comment actions Permalink
Official comment

Hello,

You don't need to address new issue id to assign values and also you should assign values in another way, this should work:

var issue = ctx.issue;
var newIssue = ctx.issue.copy();
newIssue.fields.Affected = issue.fields.Clversion;
var projectB = entities.Project.findByName('ProjectB');
newissueID.project = projectB;

You don't need to copy all the values, because copy() method creates a full copy of initial issue, including fields values like Product, Department, State, Priority. You'll only need to change a project. 

Hope it helps.

Comment actions Permalink

Hello, 

 

Thank for the answer. I'll try this way.

There is the reason that I copy values, because I don't need that all values would be copied, just certain ones. But I'll try to follow your advice. Thank you very much.

0
Comment actions Permalink

By default, all fields are copied after copy() method. So if you don't need all of them, you should overwrite those you don't need. 

Thank you. 

0
Comment actions Permalink

Thank you, Anastasia, for your help.

0
Comment actions Permalink

Hello Anastasia,

 

I'm probably doing something wrong, because I edited my workflow and left only your suggested method, but still, the issue moved to ProjectB doesn't get Affected versions value.

 

I edited my workflow this way:

 

var entities = require('@jetbrains/youtrack-scripting-api/entities');
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');

exports.rule = entities.Issue.action({
// TODO: give the rule a human-readable title
title: 'Clone +',
// TODO: define the custom command
command: 'clone_2',
guard: function(ctx) {
return ctx.issue.isReported;
},
action: function(ctx) {
var issue = ctx.issue;
var newIssue = ctx.issue.copy();
newIssue.fields.Affected = issue.fields.Clversion;
var projectB = entities.Project.findByName('ProjectB');
newIssue.project = projectB;
},
requirements: {
Clversion:{
type: entities.ProjectVersion.fieldType,
name: 'Client TP version'
},
Affected:{
type: entities.ProjectVersion.fieldType,
name: 'Affected versions'
},
}
});

 

Thank you in advance.

0
Comment actions Permalink

Irina,

Project A does not have "Affected versions" field, so please change the project at first and then assign the value:

var issue = ctx.issue;
var newIssue = ctx.issue.copy();
var projectB = entities.Project.findByName('ProjectB');
newIssue.project = projectB;
newIssue.fields.Affected = issue.fields.Clversion;

0
Comment actions Permalink

I've been looking for a way to do this and tested a modified version of this code (below). But I'm getting an error that the "Issue TEST-97 already exists" each time I run the command. Each time I try to apply the command it increments the ID up one number. 

Any ideas why it's not creating a copy in the TEST project with the next available ID?

--------------------------------

 

var entities = require('@jetbrains/youtrack-scripting-api/entities');
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');

exports.rule = entities.Issue.action({
// TODO: give the rule a human-readable title
title: 'Clone +',
// TODO: define the custom command
command: 'clone_2',
guard: function(ctx) {
return ctx.issue.isReported;
},
action: function(ctx) {
var issue = ctx.issue;
var newIssue = ctx.issue.copy();
var movetotest = entities.Project.findByName('Test Project');
newIssue.project = movetotest;
},
requirements: {
},
Affected:{
},
});

0
Comment actions Permalink

Hello Bjorgensen,

I'm not sure this would be a reason, but I noticed that when I copy and move issue to another project I get project prefix changed only, not the number. I.e. if issue in project A has PA-97 issue Id, so when I move it to project B it will become PB-97. So maybe you have TEST-97 issue in Test project already created...

@Anastasia, I'm not sure if it is a bug, but it is really strange to have issues created with larger number than the issues created manually. And wouldn't it be a problem when numbering will reach copied issues id numbers as it is in oposite to case Bjorgensen described?

0
Comment actions Permalink

Hello,

I can confirm that this is a bug: https://youtrack.jetbrains.com/issue/JT-46468. Please feel free to follow this issue to receive the updates. 

0
Comment actions Permalink

Hi Anastasia,

The bug ticket states that I can use Issue::createCopy() instead.

Are you saying I can replace ctx.issue.copy() with issue::createCopy() and it will work?

0
Comment actions Permalink

Hi,

Basically yes, but we don't recommend to do it due to unexpected side effects. 

0

Please sign in to leave a comment.