Copy current assignee of a ticket with hyperlink to text field?

Harsh
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.
June 21, 2023

Hello all,

I want to copy the assignee to a text field custom field. I did the normal way but it is taking just ID with the JIRA userID. I want to get the name with hyperlink. Is this achievable?

Thanks for the help

2 answers

1 accepted

0 votes
Answer accepted
Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 21, 2023

Hi, @Harsh 

Text field must be with wiki markup, and you need to use template like [~username]

Harsh
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.
June 21, 2023

Hello @Evgenii 

Thanks for the response. 

Right now I'm taking the  assignee from issue.currentassignee and then just substituting that to the text field(which is description, system field by jira). I think description is wiki markup by default? 

 

Brief code

Def assignee = issue.getcurrentassignee

Def description (system field) = 'text' + assignee. 

 

But what is happening is in description it's is taking the ID and jira assigned username, but i need the link (assignee) or just the iD of the user. 

 

Thanks for the help. 

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 21, 2023

Got it. Try next script:

String username = issue.assignee.username
String description = "dummy text" + "[~${username}]"
issue.setDescription(description)

 

Harsh
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.
June 21, 2023

Hi @Evgenii 

Unfortunately it throws error on String username 

I did for the display name  using -  

def username = sourceIssue.assignee?.displayName.toString()
def description = "text " + "username".

Although i still need to get the link in the username. Right now it gets the name. 

Thanks for the help.
Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 21, 2023

Hi, @Harsh 

Have you tried getting username with next code? 

def username = sourceIssue.assignee?.username.toString()  

.username - is standard attribute for user object, that retyrns string with username.

As for adding it into text - the main idea is, that you have to add username between [~ and ]. It generates hyperlink to user profile. Something like this can help:

def description = "text " + "[~" + username + "]"

Harsh
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.
June 21, 2023

Hi @Evgenii 

 

Will the code in last comment which mentioned. 

Quick question - let's say i have username - X123 and my display name is Harsh. Will the code take in the username or the display name?

I want the display name to Linked in the description.  Will update you once i test. 

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 22, 2023

to mention user you have to use username, but when you'll look at issue, you will see display name.

For example, difference between editor mode and after editing, when you look at description:

image.pngimage.png

image.png

Harsh
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.
June 22, 2023

Hi @Evgenii 

It worked like a charm. Thanks a lot. :) I have accepted the answer.

Thanks!

Like Evgenii likes this
Harsh
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.
June 22, 2023

Hi @Evgenii 

Follow up question 

can we do the same for 'Single select' field to text field(desc).
I am using this -

def CF = customFieldManager.getCustomFieldObjectByName('CF')

def cfVal = issue.getCustomFieldValue(CF).toString()

def desc = 'text' + cfval

Is this the correct approach?

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 22, 2023

@Harsh if you want to add value, as single text string - it will work. 

If you want to add value, as URL, for example, you'll need to wrap value in []

It will look like:

def desc = 'text' + [Link Name|cfVal]

image.png

0 votes
Harsh
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.
June 22, 2023

Hi @Evgenii 

 

Not as a link in this case.

Just want value from Single select field (whatever value is selected) to description - 'text'.

But unfortunately it is not picking the value from the single select field. 

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 22, 2023

Sorry, missed that it's single select field. Mistook it with text field.

Try: 

def cfVal = issue.getCustomFieldValue(CF).value.toString()

Harsh
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.
June 22, 2023

Hi,

I tried it already, but it is returning 'Null' value, but I have selected some value there.
Error - 'Cannot get property 'value' on null object'

Can we get the field by  'getcustomfieldbyID' ? or any workaround.


Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 22, 2023

If you want to select customfield by ID you can use such code:

Use your actual fieldId instead of 12345

def CF = customFieldManager.getCustomFieldObject(12345)

def cfVal = issue.getCustomFieldValue(CF).value.toString()

def desc = 'text' + cfval

Harsh
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.
June 22, 2023

above already tested, not working. :(

Will combine component accessor and may be it works.

Update - Tried with component accessor as well, its Not working

Harsh
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.
June 22, 2023

Also, for the suggestion 

 

If you want to select customfield by ID you can use such code:

Use your actual fieldId instead of 12345

def CF = customFieldManager.getCustomFieldObject(12345)

def cfVal = issue.getCustomFieldValue(CF).value.toString()

def desc = 'text' + cfval

I am getting the same error, Cannot get property 'value' on null object'

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 22, 2023

Don't know, what exactly you're using for testing, made +/- universal script, maybe it can help you to understand where is problem. It must print selected value from custom field in issue. I tested it in scriptrunner.

/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.issue.fields.CustomField

CustomField customFieldObject = ComponentAccessor.customFieldManager.getCustomFieldObject(12345)
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("TEST-1") as MutableIssue

LazyLoadedOption cfValue = issue.getCustomFieldValue(customFieldObject)

log.warn(cfValue.value)

image.png 

Harsh
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.
June 22, 2023

Hi,

Tried this on console, but it still returns null. The single select field has Test-1 value
Untitled.png

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 22, 2023

Press Logs :)

log.warn outputs messages to Logs tab.

Can you make a screenshot of console screen with code?

Harsh
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.
June 22, 2023

oh shoot my bad. 

yes, it is showing. But dont know in post function it is not working. 

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 22, 2023

If it's on creation transition, try to add postfunction after issue is reindexed

Harsh
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.
June 22, 2023

Ok

Harsh
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.
June 22, 2023

Hi, 
even after reindexing the whole issue and project. It is not working.
Still shows the error.

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 22, 2023

Can you show workflow transition with postfunctions? 

Of course, if it's not prohibited by security rules

Harsh
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.
June 22, 2023

It is prohibited.

But it just the same code which you sent. Just with the actual customfield ID.

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 22, 2023

Ok, one more variant of postfunction (it's not for console use). If it won't help, I fail to understand what's the reason of such behaviour:

/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser

CustomField customFieldObject = ComponentAccessor.customFieldManager.getCustomFieldObject(12345)
IssueManager issueManager = ComponentAccessor.getIssueManager()
ApplicationUser user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser() as ApplicationUser

MutableIssue issue = issue as MutableIssue

LazyLoadedOption cfValue = issue.getCustomFieldValue(customFieldObject) as LazyLoadedOption

String description = "Value from custom field: ${cfValue.value}"
issue.setDescription(description)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
Harsh
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.
June 22, 2023

hi @Evgenii 

Sadly, does not work. It throws the same issue. 

But that's not a problem. Will leave it for now, if it works with some other way will update you.

Thanks for all the time and help.
Have a great day.

Like Evgenii likes this
Harsh
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.
June 23, 2023

Hi @Evgenii 

The issue got resolved. 

I used sourceIssue instead of issue. In below line. 

def cfVal = sourceIssue.getCustomFieldValue(CF).value.toString()

 

Thanks. 

Like Evgenii likes this
Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 23, 2023

Hi, @Harsh 

So, the problem was in sourceIssue. It's weird, but great, that you resolved it! :) 

Suggest an answer

Log in or Sign up to answer