Scriptrunner console can't update customfield value

Olivier MAZEN July 9, 2018

Hello there,

 

i have a basic requierement : i want to change a customfield type from Number to text.

For that, i create a new custom field and wrote a script console (threw scriptrunner) and copy paste the value from my origin field to destination field.

 

at the end of my script i had the function setCustomFieldValue.

 

but it seems to not working becasue i didnt see any change in my log.

 

Please find my source code below:

 

SearchService.ParseResult parseResult = searchService.parseQuery(user, JQLsearch)
//checking the JQL is valid
if (parseResult.isValid())
{
SearchResults searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
listResults = searchResult.issues.collect {issueManager.getIssueObject(it.id)}

for(MutableIssue issueSearch:listResults)
{
log.debug("Issue en cours " + issueSearch.id)

def cforiginevalue = issueSearch.getCustomFieldValue(cforigine)
def cfdestinationvalue = issueSearch.getCustomFieldValue(cfdestination)

log.debug("cforiginevalue avant " + cforiginevalue.toString())
log.debug("cfdestinationvalue avant " + cfdestinationvalue)

issueSearch.setCustomFieldValue(cfdestination, cforiginevalue.toString())
issueSearch.setCustomFieldValue(cforigine, null)

log.debug("cforiginevalue aprés " + cforiginevalue)
log.debug("cfdestinationvalue aprés " + cfdestinationvalue)
}
}
else
{
log.error("Invalid JQL: " + JQLsearch)
}

 

please my log result below:

 

2018-07-09 17:03:25,655 DEBUG [jira.groovy]: Issue en cours 11689

2018-07-09 17:03:25,662 DEBUG [jira.groovy]: cforiginevalue aprés 1221211.0

2018-07-09 17:03:25,662 DEBUG [jira.groovy]: cfdestinationvalue avant 1

2018-07-09 17:03:25,662 DEBUG [jira.groovy]: cforiginevalue aprés 1221211.0

2018-07-09 17:03:25,662 DEBUG [jira.groovy]: cfdestinationvalue aprés 1

 

 

Does i miss something ? because the issueSearch.setCustomFieldValue works well on a post function in a workflow.

 

Regards,

 

Olivier

1 answer

1 accepted

0 votes
Answer accepted
Alexey Matveev
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.
July 9, 2018

Hello,

You should add the updateIssue method like this:

issueSearch.setCustomFieldValue(cfdestination, cforiginevalue.toString())
issueSearch.setCustomFieldValue(cforigine, null)

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

Also add import:

import com.atlassian.jira.event.type.EventDispatchOption
Olivier MAZEN July 10, 2018

Hello alexey,

 

its work well.

 

but i dont get it because i use the same way to reset a date in a workflow transition post function, and i didn't need a update issue  methode...

Do you know why ?

 

Thank anyway

 

Olivier

Alexey Matveev
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.
July 10, 2018

Have a look at the workflow transition. Your post function stays before store data and reindex. That is why it works. But in script console you must store and do reindex manually. That is exactly what udpateIssue does.

If you put your post function last in the list of post functions, it will not work as well. It would need the udpateIssue method

Olivier MAZEN July 10, 2018

Hello,

 

i get it, thank a lot :)

 

Olivier

Alexey Matveev
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.
July 10, 2018

You are welcome!

Olivier MAZEN July 10, 2018

Alexey,

 

I have one more question (sorry)

 

The origine field is a number field and it contains "3.2578777777E10".

My destination field is a string.

But i would like to copy in my destination field "32578777777"

Do you have any way to reach my goal?

 

If i copy the originefield.tostring() value i get the scientific mode in my destination field...

 

I am stuck here ...

 

Olivier

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 10, 2018

That's a bit of a java trick - you need to tell it how to format the string as you convert it.

Assuming that the variable myNumber is a number, the plain Java for this is below (I'm a bit fuzzy on Groovy and can't always get it right)

DecimalFormat decForm = new DecimalFormat("#");
decForm.setMaximumFractionDigits(6);

String myString = decForm (myNumber);

You probably want to change the "6" I've pasted in from my notes 

Olivier MAZEN July 10, 2018

Hi,

 

i found out how i can solved my probleme

 

i just added this line:

 

 issueSearch.setCustomFieldValue(cfdestination, String.format("%.0f", cforiginevalue))

 

so far so good :)

 

once again thank for your support

 

Olivier

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events