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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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())
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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())
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.