Set User CF value to current user

Scenario. 

  • User is editing an issue.
    • Specifically they are selecting an option on a checkbox field (cf_11501)
      • cf_11501 has 4 options
        • A
        • B
        • C
        • D
  • I would like a second custom field (cf_11502) to enter the value of the user that edited (cf_11501)
    • The purpose of this field is to track who edited this field for reporting.
    • cf_11502 is a single user field.
    • There will be additions to this to account for the other options but for the moment I'm focused on this scenario.

What I've written so far is:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser().getName()
def verifiedOn = customFieldManager.getCustomFieldObjectByName("11501")
def verifiedBy = customFieldManager.getCustomFieldObjectByName("11502")

if(verifiedOn.getFormValue() == "A"){
verifiedBy.setFormValue() == "user"
}

 

There's an issue with lines 8 & 9 but i cannot figure out how to make this work.  Any assistance would be greatly appreciated.

1 answer

1 accepted

Accepted Answer
1 vote

Hi Nathan,

I take it the idea is to get the last updater of your custom field 11501 and set him to custom field 11502, yes?

Try something like this:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def verifiedOnName = customFieldManager.getCustomFieldObject(11501).getName()
def verifiedBy = customFieldManager.getCustomFieldObject(11502)
def changeHistory = ComponentAccessor.getChangeHistoryManager().getAllChangeItems(issue)
def changeHistoryForCf = new ArrayList()


/*scan issue change history for items related to your custom field and add them to an Array List*/

for (int i = 0; i < changeHistory.size(); i++){
if (changeHistory[i].getField() == verifiedOnName){
changeHistoryForCf.add(changeHistory[i])
}
}


/*if any change items related to your custom field are found then find the latest one,
find its author and put his name to your custom field 11502*/

if (changeHistoryForCf.size() > 0){
def latestCfChange = changeHistoryForCf[0]
for (int i = 0; i < changeHistoryForCf.size(); i++){
if (latestCfChange.getCreated().getTime() < changeHistoryForCf[i].getCreated().getTime()){
latestCfChange = changeHistoryForCf[i]
}
}
issue.setCustomFieldValue(verifiedBy, ComponentAccessor.getUserManager().getUserByKey(latestCfChange.getUserKey()))
}

 

Thanks for assisting!

Got errors on 4 & 5 but resolved those.

Now getting errors on 18 & 22

 

CaptureScriptError.GIF

You can pretty much ignore those errors in this very case, because static type checking in ScriptRunner is not always accurate. More info here.

Since this is a scripted field judging by your screenshot, please change the code on line 22 to this:

return ComponentAccessor.getUserManager().getUserByKey(latestCfChange.getUserKey())

 

Made some updates and getting the following error.

 

2018-02-09 15:16:22,107 ERROR [customfield.GroovyCustomField]: *************************************************************************************
Script field failed on issue: H1Z1-3758, field: *TestUserField
java.lang.NullPointerException: Cannot invoke method getName() on null object
 at Script263.run(Script263.groovy:4)
Ivan Tovbin Community Champion Feb 09, 2018

This error means that you are most likely trying to get the name of a non-existing custom field object. Can you show your updated script?

Actually its not giving that error I had thrown quotes around 11501 and 11502 and realized that was part of my problem.  

 

Now Instead of it setting the correct user its setting the user as anonymous.

 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def verifiedOnName = customFieldManager.getCustomFieldObject(11501).getName()
def verifiedBy = customFieldManager.getCustomFieldObject(11502)
def changeHistory = ComponentAccessor.getChangeHistoryManager().getAllChangeItems(issue)
def changeHistoryForCf = new ArrayList()


/*scan issue change history for items related to your custom field and add them to an Array List*/

for (int i = 0; i < changeHistory.size(); i++){
if (changeHistory[i].getField() == verifiedOnName){
changeHistoryForCf.add(changeHistory[i])
}
}


/*if any change items related to your custom field are found then find the latest one,
find its author and put his name to your custom field 11502*/

if (changeHistoryForCf.size() > 0){
def latestCfChange = changeHistoryForCf[0]
for (int i = 0; i < changeHistoryForCf.size(); i++){
if (latestCfChange.getCreated().getTime() < changeHistoryForCf[i].getCreated().getTime()){
latestCfChange = changeHistoryForCf[i]
}
}
return ComponentAccessor.getUserManager().getUserByKey(latestCfChange.getUserKey())
}
Ivan Tovbin Community Champion Feb 09, 2018

Check the searcher for this custom field. I'd wager it's set to 'Free Text Searcher'. Set it to 'User Picker Searcher'. After that run a re-index. This should solve your issue. 

Nailed it.

 

Much Appreciated again.!!!

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 11, 2018 in Marketplace Apps

You + one app + a desert island...

Hi all! My name is Miles and I work on the Marketplace team. We’re looking for better ways to recommend and suggest apps that are truly crowd favorites, so of course we wanted to poll the Community. ...

2,829 views 5 6
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you