Forums

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

Copying custom field values

vikranthcms September 27, 2021


I have two custom fields CF1 and CF2

CF1 is a cascading select field & CF2 is single select field

CF1 has values like parent value P1 and child values 1,2,3
Parent value P2 with Child values 4,5,6,

I want to Sync the CF1 and CF2 fields

If CF1 have value (parent value)P1 ,(child value)1 then CF2 should be synced to "P1 - 1" which I was able to achieve with the script I found, but
If parent value is selected then CF2 value is not updated as the script checks with the Child value 

Second part is to sync the CF2 value to CF1 not working
I need to achieve the below

If I modify CF2 and has the value "P1 - 1" then it need to update the CF1 to P1 in the Parent and 1 in the child and If the child value is "P1 -None"
it should update the CF1 Parent value only.

 

Hope my question is clear!

Thankyou in advance!!

 

Regards,

Vikranth.A

1 answer

0 votes
John Funk
Community Champion
September 27, 2021

Hi @vikranthcms  - Can you share the rule that you have so far?

vikranthcms September 27, 2021

Hello John

I am using Server/Datacenter one

Please find the Script used to Copy the cascade field to Select field below.

In the below script If I select only the Parent value and child value is not selected in CF1 then CF2 does not get effect.


import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.fields.config.FieldConfigScheme

import com.atlassian.jira.issue.context.ProjectContext

import groovy.util.logging.Log4j

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.event.issue.IssueEvent

import com.atlassian.jira.issue.MutableIssue

 

//Look for Events relating to this field

 def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "CF1"}

def eventType = event.eventTypeId

if (change || eventType == 1) {

 

                try

                                {

 

                                def issueManager = ComponentAccessor.getIssueManager()

                                def Issue issue = event.issue

                                def fieldConfigSchemeManager = ComponentAccessor.fieldConfigSchemeManager

                                def customFieldManager = ComponentAccessor.customFieldManager

 

                               

                                def vsSVScascade = customFieldManager.getCustomFieldObjectByName("CF1")

                                def selectedOptions = issue.getCustomFieldValue(vsSVScascade) as Map

                                def parentOptionLevel = null

                                def childOptionLevel = '1'

 

                                def selectedParentValue = selectedOptions.get(parentOptionLevel).toString()

                                def selectedChildValue = selectedOptions.get(childOptionLevel).toString()

 

                                def cascadeOptionSelected = selectedParentValue + " - " + selectedChildValue

 

                                                                // the name of the custom field (single select list type)

                                def customFieldName = 'CF2(select)'

 

                               

                                def customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)

                               

 

                                def availableOptions = ComponentAccessor.optionsManager.getOptions(customField.getRelevantConfig(issue))

                                def optionToSet = availableOptions.find { it.value == cascadeOptionSelected }

                               

                                if (optionToSet){

                                customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder())

                                log.warn("Select Changed")

                }

    else{

        //assert optionToSet: "Could not find option with value $cascadeOptionSelected. Available options are ${availableOptions*.value.join(",")}"

        log.warn("Failed as didnt find value")

    }

}

   

    catch (error) {

  log.warn("Failed: " + error)

}

}

else{

    log.warn("Did not run")

}


 

Thanks,Vikranth.A

vikranthcms September 29, 2021

Hey can you provide me the script for copying only parent value in CF1  to the CF2 

vikranthcms October 1, 2021

Hello John,

 

Can you help me in writing the script in the reverse order, that is 

From the CF2(Select field ) to CF1(Cascading field)

I want to achieve the below

 

CF2 Has the Value P1-1and we have to copy the P1 value to parent of the cascading and 1 to the Child value

and 

If CF2 has a Value P1 only then we have to copy the P1 value to parent of the cascading and the default none value to the child 

 

Thanks,

Vikranth

Suggest an answer

Log in or Sign up to answer