Scriptrunner - Post Function - Clone and Issue, and links

Nicolás Figueroa January 13, 2021

Hello,

Im creating 5 automatic Issuelinks with a PostFunction Scriptunner (Clone and Issue, and links) with that everythink is okey, the problem is I want to perform a "Aditional issue action" where (here is de difficult part) Want to copy a value from the Main Issue, but this customfield(text field single line) have a formated input like (CODE=VALUE;CODE=VALUE;...), in the linked issues need to sum each VALUE and then assign 20% of the sum in each issuelink.

Not sure if you this is posible from there, maybe other kind of function can resolve this.

From now thanks and have a nice day.Where I want to put the code.png

1 answer

1 accepted

1 vote
Answer accepted
mogavenasan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 13, 2021

Hi @Nicolás Figueroa,

I hope that my understanding is correct:

  1. Parent issue will have a custom field with value as per the following; CODE=VALUE;CODE=VALUE;...
  2. For the issues that we are automating the creation, we would like to process the string and assign 20% of the sum to each linked issues.
  3. So basically what the code needs to do is to get the content of the field, sum all the VALUE and then assign 20% of the sum to each linked issue.
  4. The custom field is the same for the parent and child issues.

Correct me if I'm getting anything wrong here. If I got it all right, then this should be script:

def cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'custom field name'}
def cfValues = sourceIssue.getCustomFieldValue(cf)
def cfValue = cfValues.split(';')

float value


for ( String item : cfValue ) {
value = item.split('=')[1]
value += value
}

def finalValue = value * 0.2


issue.setCustomFieldValue(cf, finalValue)

FYI, I didn't test the script. I hope that this helps.

Thanks,
Moga

Nicolás Figueroa January 15, 2021

Hello @mogavenasan 

Thanks for your quick response and yes you are undestading well the case.

I try the script and change the value for String because its give error (cause float value = String item) and then convert it a float, but im not sure if im doing it well.

Also the script gives this error:

script split.png

Nicolás Figueroa January 15, 2021

Updated: now have no error, but the field its not updated:

 

def cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'custom field name'}
def cfValues = sourceIssue.getCustomFieldValue(cf) as String
def cfValue = cfValues.split(';')

float values


for ( String item : cfValue ) {
def value = item.split('=')[1]
values = value as Float
values += (float) values
}

def finalValue = values * 0.2


issue.setCustomFieldValue(cf, finalValue)
Nicolás Figueroa January 20, 2021

After some modification I can make it run:

def cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Custom FieldName'}
def cfValue = sourceIssue.getCustomFieldValue(cf)
//Use string array to hold all values and convert them to String
String[] cfValues = cfValue.toString().split(';')

double values = 0.0

for ( String item : cfValues ) {
//Convert the VALUES to duble
Double value = Double.parseDouble(item.split("=")[1])
values += value
}

//Convert to string because the customfield is SimpleText
def finalValue = String.valueOf(values * 0.2)

issue.setCustomFieldValue(cf, finalValue)
Like mogavenasan likes this
Teja June 2, 2021

Hi @Nicolás Figueroa 

You have mentioned "Im creating 5 automatic Issuelinks with a PostFunction Scriptunner (Clone and Issue, and links)", How have you been creating 5 issue links with the single post-function? or you have used 5 post-function.

Thanks

Nicolás Figueroa June 3, 2021

Hello @Teja

I have created 5 different post-functions with Scriptrunner.

Maybe you can create only 1 post-function that create 5 issuelinks but I believe you need to write some code.

Teja June 3, 2021

@Nicolás Figueroa 

Ok sure, thanks

Suggest an answer

Log in or Sign up to answer