Set Current User to Single User Picker Custom Field on Customer Portal

Alvin
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.
October 1, 2018

Hi Community, Can you help me regarding with my problem?

I have a User Picker Custom Field on Customer Portal, I want it to be default by catching who is the current user and place it on that custom field.

Is it possible using scriptrunner? and on what part of scriptrunner? Scripted Fields? Post Functions? Any help is really appreciated, if you do have some code to start with, that would be great. Thanks!

 

3 answers

5 votes
Mark Markov
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.
October 2, 2018

Hello @Alvin

You can achieve it via script postfunction. Here code example

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def customFieldManager = ComponentAccessor.getCustomFieldManager()

// a user custom field
def userCf = customFieldManager.getCustomFieldObjectByName("User Picker Customfield Name")
def currenrUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issue.setCustomFieldValue(userCf, currenrUser)
ComponentAccessor.getIssueManager().updateIssue(currenrUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 2, 2018

Just a small suggestion, I personally prefer to use

customField.updateValue method to update custom field value on Issue instead of issue.setCustomFieldValue because then the "updateIssue" statement is not required.

setCustomFieldValue

void setCustomFieldValue(CustomField customField,
                         Object value)

Sets a custom field value on this Issue Object, but does not write it to the database. This is highly misleading. 
To actually set a custom field value, use OrderableField.updateIssue(com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem, MutableIssue, java.util.Map)

Parameters:
customField - the CustomField
value - the value.
Alvin
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.
October 2, 2018

Hi @Mark Markov, it still not catch the current user on the customer portal.

Screenshot from 2018-10-02 17_11_37.png

Alvin
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.
October 2, 2018

@Tarun Sapra@Mark Markov I put the post function at the very beginning

Mark Markov
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.
October 2, 2018

Postfunction will update field value after issue will be created.

If you want to set value right on portal interface, you can use scriptrunner behaviours.

@Tarun SapraIn my opinion, i would not recommend to use updateValue, because it save changes directly in database and changes will not appear in the issue history.

I think this is really bad practice, when issue changes without saving history. Because when something happens, it will be really hard to find out cause of problem.

IT User Support October 2, 2018

Hi @Mark Markov , I already try to use it on behavior. but it just capture current user on agent screen , edit/view screen.

IT User Support October 2, 2018

@Mark Markov , I need to have default value , just like 

Request this in behalf of:

IT User Support October 2, 2018

def users = getFieldById("customfield_xxxxx)

def currentuser = ComponentAccesor.getJiraAuthenticationContext().getLoggedInUser().getName()

users.setFormValue(currentuser) 

 

I put it on Initialiser. because I just want to have that user picker field a default value when they are creating a ticket., but still no luck

Am I doing it right @Mark Markov @Tarun Sapra

Mark Markov
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.
October 2, 2018

As i remember, you need to provide key, not name

import com.atlassian.jira.component.ComponentAccessor
def users = getFieldById("customfield_xxxxx")

def currentuser = ComponentAccesor.getJiraAuthenticationContext().getLoggedInUser().getKey()

users.setFormValue(currentuser) 

Are you map this behaviour to Service Desk?

Like Markus Pöhler likes this
IT User Support October 2, 2018

@Mark Markov , yes I mapped it on my test project., I will your code tomorrow , Thank you for the help. will uodatu you tomorrow. 

IT User Support October 2, 2018

I will try the code tomorrow. will update you too. thanks

IT User Support October 2, 2018

Hi @Mark Markov, I tried it now but still no luck. do we have any other options?

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 2, 2018

Hello @Mark Markov

Indeed, if history changes and other permission(workflow etc) checks are a priority then using the IssueServcie is the safest bet which doesn't violate the permission checks as well unlike the issueManager. updateIssue

https://developer.atlassian.com/server/jira/platform/performing-issue-operations/

IT User Support October 2, 2018

Hi @Tarun Sapra , do we have any other options beside behavior?

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 2, 2018

Hello @Alvin

Since you want the value to be populated before issue creation thus Behaviours is the option that comes to my mind as well, can you please try this code

import com.atlassian.jira.component.ComponentAccessor
def affectedUser = getFieldByName("<You custom field name here>")

def currentuser = ComponentAccessor.jiraAuthenticationContext.loggedInUser.username

affectedUser.setFormValue(currentuser) 
Like Carol Jones likes this
Alvin
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.
October 2, 2018

Hi @Tarun Sapra@Mark Markov , here is I want to achieve. Screenshot from 2018-10-03 11_29_58.pngI tried your code but it does not update the value of requested for field in customer portal. any chance? 

Carol Jones September 17, 2019

I just tested what @Tarun Sapra posted on Oct 2, 2018 and its working for me.  Just a side note, when mapping the behavior to the specific project, be sure to select the Service Desk option as they have split-out the Service Desk and Jira mapping options.  I'll also note that the behaviors functionality only recently (within the last year or so) became available on the Service Desk side, I don't remember which version.  So it might have been that it only had the Jira capabilities with the version you had installed.  But hopefully you have a more recent version now and have it working!  Thanks!!

Mike_Low May 15, 2020

Thanks Mark Markov  the example in your first comment got me what I needed. Got a warning but but still works!2020-05-15_17-02-37.png

0 votes
Alvin
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.
October 2, 2018

@Tarun Sapra@Mark Markov I am getting this error on post-script function

 

Time (on server): Tue Oct 02 2018 17:29:55 GMT+0800 (+08)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2018-10-02 09:29:54,032 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-10-02 09:29:54,049 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.IllegalArgumentException: Source GenericValue can not be null.
 at com.atlassian.jira.association.NodeAssociationStoreImpl.getSinksFromSource(NodeAssociationStoreImpl.java:34)
 at com.atlassian.jira.project.version.DefaultVersionManager.getVersionsByIssue(DefaultVersionManager.java:752)
 at com.atlassian.jira.project.version.DefaultVersionManager.getFixVersionsFor(DefaultVersionManager.java:598)
 at com.atlassian.jira.project.version.DefaultVersionManager.updateIssueFixVersions(DefaultVersionManager.java:568)
 at com.atlassian.jira.issue.fields.FixVersionsSystemField.updateIssueValue(FixVersionsSystemField.java:98)
 at com.atlassian.jira.issue.fields.AbstractVersionsSystemField.updateValue(AbstractVersionsSystemField.java:368)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:708)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:673)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:659)
 at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:214)
 at com.atlassian.jira.issue.IssueManager$updateIssue$0.call(Unknown Source)
 at Script978.run(Script978.groovy:10)
0 votes
Alvin
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.
October 1, 2018

Suggest an answer

Log in or Sign up to answer