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!
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)
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.
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)
customField
- the CustomFieldvalue
- the value.You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Mark Markov, it still not catch the current user on the customer portal.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Tarun Sapra@Mark Markov I put the post function at the very beginning
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Mark Markov , I already try to use it on behavior. but it just capture current user on agent screen , edit/view screen.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Mark Markov , yes I mapped it on my test project., I will your code tomorrow , Thank you for the help. will uodatu you tomorrow.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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/
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Tarun Sapra@Mark Markov , here is I want to achieve. I tried your code but it does not update the value of requested for field in customer portal. any chance?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Mark Markov the example in your first comment got me what I needed. Got a warning but but still works!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
YES, I CONFIRM, in DC this works under Initialiser, with mapping set to service desk
import com.atlassian.jira.component.ComponentAccessor
def affectedUser = getFieldByName("<You custom field name here>")
def currentuser = ComponentAccessor.jiraAuthenticationContext.loggedInUser.username
affectedUser.setFormValue(currentuser)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.