Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Listener : To copy custom field value

Akhil_vs
Contributor
December 18, 2018

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

3 answers

0 votes
Akhil_vs
Contributor
December 20, 2018

Hello Team,

 

Is there anyone who can help me  with this script listener part.

 

Thanks,

Akhil

0 votes
Akhil_vs
Contributor
December 18, 2018

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

Ismael Jimoh
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.
December 20, 2018

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

Deleted user February 7, 2019

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

Ismael Jimoh
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 7, 2019

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.

Deleted user February 7, 2019

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

Ismael Jimoh
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 7, 2019

Hi @[deleted],

But this has to be also done in the Script ? Isn't it ? 

  • Yes, this is to be added to the script. You pull the value of the request participant field and your field and compare them.
    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)

     

  • The code above would normally show you which users are in one field and not the other so it can help you start as a template.

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

  • If you are going to be applying the same in future then your choice may be limited to either adding your script to each workflow for the new projects or better yet, using listeners.

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. 

  • I meant adding your script to a workflow transition. The reason why is this ensures that until the transition is performed, the copy would not occur. You can then pair this with the suggestion to check if the value of the fields are the same before the action is performed.
  • By the way, the value of a request participant should be copied easily if the script is the last action run in your post-function.

 

Deleted user February 7, 2019

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

Deleted user February 12, 2019

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

Like Ismael Jimoh likes this
0 votes
Ismael Jimoh
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.
December 18, 2018

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.

Suggest an answer

Log in or Sign up to answer