Hello Team,
I have a requirement to copy the value from a dropdown custom field to a number custom field, when issue is edited.
Could anyone please help me with the scriptrunner listener script for doing this task.
Thanks,
Akhil
Hello Team,
Is there anyone who can help me with this script listener part.
Thanks,
Akhil
Hello Ismael,
Thanks for your response. I have made up a groovy script as shown below:
But I am not able to fetch the values from drop down custom field and assign it to number field.
Any help is highly appreciated.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.MutableIssue
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.getIssueManager()
import com.atlassian.jira.issue.Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def SP_1 = customFieldManager.getCustomFieldObject("14318")
def SP_2 = customFieldManager.getCustomFieldObject("10002")
def issue = event.issue as Issue
MutableIssue issueToUpdate = (MutableIssue) issue;
issueToUpdate.setCustomFieldValue(SP_2, SP_1);
Thanks,
Akhil
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No what you have done above would likely not work.
You need the value from the field not the field and what you are doing is just getting custom field object. I do not see you pulling the value itself out.
def SP_1 = customFieldManager.getCustomFieldObject("14318") //Not sure why you are getting this I assume you are attempting to get the customfield itself
def SP_2 = issue.getCustomFieldValue("10002")?.value //Assuming this is your drop down
As for parsing the value, what is issueToUpdate?
If it is the same issue then you should be parsing it in the form of
issue.setCustomFieldValue(SP1, SP2)
issueManager.updateIssue(user, issue, EventDispatchOpton.ISSUE_UPDATED,false)
This is a sample of what you really need to do.
Recommend looking at samples at the Scriptrunner page itself
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ismael,/ Experts here in Community
may be you can help me here. We want in Our sevice Desk Projects Issue Security that only Creator/ Request Participant and group of SD Agents should see the Ticket and not all.
As of now with Jira its not possible for Request Participant. I have created a Script Listner which would copy the Request Participant to one Custom Field and this Custom Field can be used in Issue Security page.
As of now its in Test Project and test is successful. I have assigned thr Script Listner to "Issue Update" event because a event such as "Request Participant changed" is not in Jira. My worry is :: Will it have the performance Problems because it will be triggerd with every Issue Update ?
Thanks in Advance.
PreetiD
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes it will.
Each time the issue in question is updated, it would repopulate it with the values because the script runs.
Instead of this, I would do a check of the values from both fields(request participant vs your custom field) first and exit the script if the values match.
Remember to restrict the event to your project to avoid it triggering for all projects.
A better solution would be add the script to your workflow where by when the status changes, the list of participants is repopulated.
This should be less system intensive than triggering an event from my experience.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey thanks for replying so fast.
Instead of this, I would do a check of the values from both fields(request participant vs your custom field) first and exit the script if the values match.
****But this has to be also done in the Script ? Isn't it ?
Remember to restrict the event to your project to avoid it triggering for all projects.
****This is for sure. But will be looking for the Solution in future if more Projects want the same, till Jira takes Request Participant into the Issue Security page.
A better solution would be add the script to your workflow where by when the status changes, the list of participants is repopulated.
**** Again thanks for this Idea too but Adding the Participant is not having any dependencies on the Field Change. The Customers/Users/Agents can add the Participants any time when the Issue is still in the same Status. And here it will be difficult to take the new participants added to the custom field if its somewhere in Post Function of the Workflow.
Any more suggestions ? Even if I create a new Event, it has to be associated with existing template. And more passing Template is "Issue Updated" :(
Thanks,
PreetiD
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @[deleted],
But this has to be also done in the Script ? Isn't it ?
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
cf = customFieldManager.getCustomFieldObject('<reqPart-field-id>')
hcf = customFieldManager.getCustomFieldObject('<yourCustomField>')
cfVal = myIssue.getCustomFieldValue(cf)
hcfVal = myIssue.getCustomFieldValue(hcf)
log.warn("Your Field Value"+hcfVal+ " and Request Participant Field Value: "+cfVal)
int numOfVals = cfVal.size()
int numOfHfVals = hcfVal.size()
Set userDiff = new HashSet()
Set userDiffEmail = new HashSet()
def participantName
for (int i = 0; i < numOfVals ; i++)
{
for(int j = 0; j < numOfHfVals; j++){
if(hcfVal[j] == cfVal[i]){
}
else{
userDiff.add(cfVal[i])
}
}
}
log.warn(userDiff)
This is for sure. But will be looking for the Solution in future if more Projects want the same, till Jira takes Request Participant into the Issue Security page
Again thanks for this Idea too but Adding the Participant is not having any dependencies on the Field Change. The Customers/Users/Agents can add the Participants any time when the Issue is still in the same Status. And here it will be difficult to take the new participants added to the custom field if its somewhere in Post Function of the Workflow.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, thanks a lot for the Script. Below is what I was using in Script Listner, which is also updating the values correctly.
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Custom-Participant"}
def calcField = customFieldManager.getCustomFieldObjectByName("Request Participants")
def changeHolder = new DefaultIssueChangeHolder()
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), issue.getCustomFieldValue(calcField)),changeHolder)
The problem is only performance Issue as its with every Issue Update event.
I don't want to go with Post Function actually, though it is simpler, because the Paricipant changes are dynamic and the copying should also take place dynamically and not after changing some status with the Transitions and copying the value with Post Function.
Upon searching in Script Runner a bit, I have got one more Idea. Creating the JSON Job/ Escalation Service to copy the value to the Custom Field and make it run every 10/ 15 min for particular project. And pasting your script there. Only disadvantage is, it will not be runtime with change of Paricipant value but at least it will update values in every 15 min if any. May be better for performance.. What's your opinion on this?
Thanks,
PreetiD
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This was directly possible with the help of Automation for Jira Add-On. There is directly the ChangeEvent for Request Participant field and you can modify the values of Fields in the Issues on this ChangeEvent.
Works totally as expected and the probem of Issue Security for Request Participant is solved by this way.
Thanks,
PreetiD
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
There are no listeners that directly do what you want if I recall correctly.
Instead, do the following:
# Create an issue updated event Listener.
# In the script section, add a script that pulls the value of your drop down custom field.
# Parse this variable to an update field value to update your number field.
These are the steps needed.
Look up how to get values from a select list and update an issue in the official documentation from Scriptrunner.
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.