It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

1 vote
Mark Markov Community Leader Oct 02, 2018

Hello @Alvin Parreno

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)

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.

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

Screenshot from 2018-10-02 17_11_37.png

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

Mark Markov Community Leader Oct 02, 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.

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

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

Request this in behalf of:

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

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

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

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

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

Tarun Sapra Community Leader Oct 02, 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/

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

Hello @Alvin Parreno

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

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? 

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!!

@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)

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

343 views 1 3
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you