Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Setting label via post-function not working reliably

Julian Rdnz
Contributor
January 29, 2018

Hi all,

I have a script which generates a value <Year>-<CalendarWeek> and adds it as label. This is done as post-function during a transition using "Code Runner" plugin.

Unfortunately, this doesn't work reliably. If you perform the transition on an issue the first time, it doesn't set a label. Do it the second time and the label will be set.

What's the trick here?

import java.util.Set
import java.util.Calendar;
import java.text.DecimalFormat;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.label.*
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.util.ImportUtils
import org.apache.log4j.Category


LabelManager labelManager = ComponentAccessor.getComponent(LabelManager.class);

JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext();

def issueManager = ComponentAccessor.getIssueManager()

DecimalFormat df = new DecimalFormat("00.##");
Calendar cal = Calendar.getInstance();
int CalendarWeek = cal.get(Calendar.WEEK_OF_YEAR);
int CalendarYear = cal.get(Calendar.YEAR);
String CalendarWeekLabel = String.valueOf(CalendarYear) + "-" + df.format(CalendarWeek);

com.atlassian.jira.user.ApplicationUser myUser = jiraAuthenticationContext.getLoggedInUser();
$log.warn( CalendarWeekLabel + "to set as label.");
labelManager.addLabel(myUser, $issue.getId(), CalendarWeekLabel, false);
$log.warn( CalendarWeekLabel + "set as label.");

3 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Kristian Walker _Adaptavist_
Community Champion
September 26, 2018

Hi Asha,

Thank you for confirming this.

I can confirm that to set the value for a field which is not on screen that you will need to override the overrideScreenSecurity query paramater in the Rest call where you are setting the field and this will require the Script Listiner to be ran as the ScriptRunner Add On user. 

We have an example of how to set this paramter inside of our documentation example which is located here and you can use this as a reference for how to set this paramater. 

If this does not resolve the issue could you please try setting  the select list value using the syntax shown in the example code snippet located here.

This example can be ran on the Script Console and shows how you can update an issue and set a select list value on an issue.

You will be able to use this example in order to modify your script to set the select list field that you require.

If this response has answered your question can you please mark it as accepted so that other users can see it is correct when searching for similar answers.

Regards,
Kristian

Asha Kumari
September 27, 2018

Kristian,

Thank you very much for sharing the code snippet. While my code was almost same as the one recommended by the piece of code block you shared, comparing the two blocks of code side by side, helped me find one logical error I had in my code.

This is all the problem was:

fields: [
"{$urgencyField.key}": [
value: "${urgencyValue}"
]
]

had to be changed to:

fields: [
"${urgencyField.key}": [
value: "${urgencyValue}"
]
]

 

Now it is working absolutely fine. Thank you for all your help to get me here :)

0 votes
Daniel Yelamos [Adaptavist]
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 Champions.
September 24, 2018

Hi Asha.

Is there any particular reason why you are sending REST request instead of using the class methods? I haven't setup this particular example but this:

def severityField = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == 'Severity'
} as Map

Can be done like this:

import com.atlassian.jira.component.ComponentAccessor
def
severityField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Severity")

----

This:

def urgencyField = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == 'Urgency'
} as Map

Can be done like this:

import com.atlassian.jira.component.ComponentAccessor
def urgencyField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Urgency")

----

And this, which I suppose is a customField setter of an issue value:

def result = put("/rest/api/2/issue/${issueKey}")
.queryString("overrideScreenSecurity", Boolean.TRUE)
.header('Content-Type', 'application/json')
.body([
fields: [
"{$urgencyField.key}": [
value: "${urgencyValue}"
]
]
])
.asString()

Can be done like this:

issue.setCustomFieldValue(urgencyField, urgencyValue)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

----

Have you tried any of these?

Cheers!

DY

Asha Kumari
September 24, 2018

For whatever reason, I am not able to import the libraries like this in my script:

import com.atlassian.jira.component.ComponentAccessor

 

It keeps giving me error no matter which library I am trying to import. I tried a lot and then decided to keep it simple using APIs that probably allows me to do everything required. Haven't been successful either way though.

Asha Kumari
September 25, 2018

jira_script_listener_import_error.png

This is the error I keep getting for any library import

Daniel Yelamos [Adaptavist]
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 Champions.
September 25, 2018

Hi Asha. 

Where it is that you are calling this script?

Cheers!

DY

Asha Kumari
September 25, 2018

I have tried this in 2 modes and both have same problem:

  1. Post script on status change (this actually is not the right place for me to plug in but I have tried this as well)
  2. Script listener which is configured to be triggered for a specific project on 'Issue Updated' event.

Screenshot for the reference

script_listener.PNG

 

Daniel Yelamos [Adaptavist]
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 Champions.
September 26, 2018

Hi Asha.

I would like to extend my apologies. I didn't see the jira-cloud tag in the post, you don't mention that this is for Scriptrunner Cloud in the description of your question. That led me to believe this was for Jira Server. my previous comments were misleading and sadly, I have no experience with Jira Cloud, so I can't help you there.

I'm so very sorry, your original script is possibly right. 

Maybe someone that knows Cloud can help you out. 

Once again, apologies.

DYelamos

Asha Kumari
September 26, 2018

No problem Daniel. Thank you for your attempt. I also updated the issue description to call out that I am using cloud version. Hope someone helps me out with this.

Kristian Walker _Adaptavist_
Community Champion
September 26, 2018

Hi Asha,

I can confirm that to set the value for a field which is not on screen that you will need to override the overrideScreenSecurity query paramater in the Rest call where you are setting the field and this will require the Script Listiner to be ran as the ScriptRunner Add On user. 

We have an example of how to set this paramter inside of our documentation example which is located here and you can use this as a reference for how to set this paramater. 

Also can I please ask what type of field is customfield_10406 so that we can verify that you are specifying the correct syntax to set the field as this error could also be caused if the script cannot recognise the field and how to set it. 

Regards,

Kristian

Asha Kumari
September 26, 2018

Hi Kristian,

Thanks for trying to help on this. customfield_10406 is of select list type. The field from which I am deriving the value is also of the same type.

Kristian Walker _Adaptavist_
Community Champion
September 26, 2018

Hi Asha,

Thank you for confirming this.

Can you please confirm if overriding the screen  security query paramater resolves the issue and allows the field to be set. 

If this does not resolve the issue could you please try setting  the select list value using the syntax shown in the example code snippet located here.

This example can be ran on the Script Console and shows how you can update an issue and set a select list value on an issue.

You will be able to use this example in order to modify your script to set the select list field that you require.

If this response has answered your question can you please mark it as accepted so that other users can see it is correct when searching for similar answers.

Regards,
Kristian

Asha Kumari
September 26, 2018

Kristian,

If you noticed the code block I have shared, override screen security parameter is already set but that hasn't helped the situation. Let me try the other code snippet you have shared and get back to you on how things go.

Asha Kumari
September 26, 2018

Kristian,

Thank you very much for sharing the code snippet. While my code was almost same as the one recommended by the piece of code block you shared, comparing the two blocks of code side by side, helped me find one logical error I had in my code.

This is all the problem was:

fields: [
"{$urgencyField.key}": [
value: "${urgencyValue}"
]
]

had to be changed to:

fields: [
"${urgencyField.key}": [
value: "${urgencyValue}"
]
]

 

Now it is working absolutely fine. Thank you for all your help to get me here :)

Kristian Walker _Adaptavist_
Community Champion
September 26, 2018

Hi Asha,

Thank you for confirming that the issue has been resolved.

Could you please accept this answer to mark it as correct and help others find it who are searching for a similar question in future.

Regards,

Kristian

Asha Kumari
September 26, 2018

Kristian,

I am not able to do so for the specific comment that you have made. I am guessing the reason is because this is a comment under another comment and it doesn't make sense to mark that as accepted answer. Would you mind adding a separate comment with the same information provided earlier, so that I can flag that as the answer?

Kristian Walker _Adaptavist_
Community Champion
September 26, 2018

I have re added this as an answer below.

Regards,

Kristian

0 votes
Asha Kumari
September 23, 2018

Can someone please help on this? I am stuck with this and not been able to make progress. Tried a lot of suggestions on this forum and none has helped so far.

TAGS
AUG Leaders

Atlassian Community Events