Set User CF value to current user

Nathan Parmeter February 9, 2018

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

1 vote
Answer accepted
Ivan Tovbin
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.
February 9, 2018

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()))
}

 

Nathan Parmeter February 9, 2018

Thanks for assisting!

Got errors on 4 & 5 but resolved those.

Now getting errors on 18 & 22

 

CaptureScriptError.GIF

Ivan Tovbin
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.
February 9, 2018

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

 

Nathan Parmeter February 9, 2018

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
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.
February 9, 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?

Nathan Parmeter February 9, 2018

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
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.
February 9, 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. 

Nathan Parmeter February 9, 2018

Nailed it.

 

Much Appreciated again.!!!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events