Copy Value from cascading list to custom field

Hi,

I am trying to copy first value of cascade select list to a custom text field "Business" and second value to security level.

I was earlier using below groovy on create issue post function to copy first value of cascade list to security level.

import org.apache.log4j.Category
import com.opensymphony.user.User
import com.googlecode.jsu.util.WorkflowUtils
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.ComponentManager
import org.ofbiz.core.entity.GenericValue


def log = Category.getInstance("com.onresolve.jira.groovy.MriskCondition")

ComponentManager componentManager = ComponentManager.getInstance()
IssueSecurityLevelManager securityLevelManager = componentManager.getComponentInstanceOfType(com.atlassian.jira.issue.security.IssueSecurityLevelManager)

IssueSecuritySchemeManager issueSecuritySchemeManager = ManagerFactory.getIssueSecuritySchemeManager()
IssueSecurityLevelManager issueSecurityLevelManager = ManagerFactory.getIssueSecurityLevelManager()

GenericValue srcProjectGV = issue.getProject()
log.debug(srcProjectGV)
def issueSecurityScheme = issueSecuritySchemeManager.getSchemes(srcProjectGV).size() == 0 ? null : issueSecuritySchemeManager.getSchemes(srcProjectGV)[0]

User currentUser = componentManager.getJiraAuthenticationContext().getUser()

def sourceFieldName = "customfield_10572"

def sourceFieldVal = WorkflowUtils.getFieldValueFromIssue(issue, WorkflowUtils.getFieldFromKey(sourceFieldName))
sourceFieldVal = sourceFieldVal.getKeysAndValues()


if ( sourceFieldVal != null ) {
    def business = sourceFieldVal[null][0]
    log.debug("business: '$business'"  )
    def secLevelGvList = issueSecurityLevelManager.getUsersSecurityLevels(issue.getGenericValue(), currentUser)
    log.debug(secLevelGvList)
    def secLevelGv = secLevelGvList.find ({  it.get("name") == "$business"})
    log.debug(secLevelGv)

    if (secLevelGv) {
        log.debug ("Set ${issue.getKey()} to $business")
        issue.setSecurityLevel (secLevelGv)
        issue.store()
    }

}

Now I want to copy first value to a text custom field and secend value to security level.

Could you please modify above script to solve my problem ?

2 answers

1 accepted

Accepted Answer
0 votes

Resolved using

cfBusiness.createValue(issue, business.toString())

Try this. I haven't tested it so you might need to do a few tweaks, but shows the idea.

import org.apache.log4j.Category
import com.opensymphony.user.User
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.ComponentManager
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.fields.CustomField
 
 
def log = Category.getInstance("com.onresolve.jira.groovy.MriskCondition")
 
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()

IssueSecurityLevelManager securityLevelManager = componentManager.getComponentInstanceOfType(com.atlassian.jira.issue.security.IssueSecurityLevelManager)
 
IssueSecuritySchemeManager issueSecuritySchemeManager = ManagerFactory.getIssueSecuritySchemeManager()
IssueSecurityLevelManager issueSecurityLevelManager = ManagerFactory.getIssueSecurityLevelManager()
 
GenericValue srcProjectGV = issue.getProject()
log.debug(srcProjectGV)
def issueSecurityScheme = issueSecuritySchemeManager.getSchemes(srcProjectGV).size() == 0 ? null : issueSecuritySchemeManager.getSchemes(srcProjectGV)[0]
 
User currentUser = componentManager.getJiraAuthenticationContext().getUser()
 
def sourceFieldName = "customfield_10572"
 
CustomField cf = customFieldManager.getCustomFieldObjectByName(customfield_10572)
CustomFieldParams sourceFieldVal = issue.getCustomFieldValue(cf) as CustomFieldParams
CustomField cfBusiness = customFieldManager.getCustomFieldObjectByName("Business")
if (sourceFieldVal) {
    Collection values = sourceFieldVal.getAllValues()
    def business = values[0]
    def level = values[1]
	
	//set the text field
	issue.setCustomFieldValue(cfBusiness, business)
	
	log.debug("business: '$business'"  )
	
	//set security level
    def secLevelGvList = issueSecurityLevelManager.getUsersSecurityLevels(issue.getGenericValue(), currentUser)
    log.debug(secLevelGvList)
    def secLevelGv = secLevelGvList.find ({  it.get("name") == "$level"})
    log.debug(secLevelGv)
	
	 if (secLevelGv) {
        log.debug ("Set ${issue.getKey()} to $level")
        issue.setSecurityLevel (secLevelGv)
        issue.store()
    }   
}
else {
    log.debug("Custom field not present on this issue")
}

It's copying the second list value (level) to security level but it's still not copying the first list value (business) to custom field (text field type).

No error reported in logs.

Used above changes and got this error:

The script failed : groovy.lang.MissingPropertyException: No such property: params for class: com.atlassian.jira.issue.customfields.view.CustomFieldParamsImpl

using this will update security level but not custom field value.

def business= sourceFieldVal.getFirstValueForNullKey()

def level = sourceFieldVal.getFirstValueForKey("1")

Okay try using :

def business= sourceFieldVal.getFirstValueForNullKey()
def level = sourceFieldVal.params.getFirstValueForKey("1");

Actually the previous version of the code should work also. Can you print the value of business variable and see what it is ?

I think problem is not with getting the values but it's with setting the custom field value. I tried using level value for setting custom field value and security level both. Security level worked fine but customfield did not.

Have you checked if cfBusiness variable is not null ?

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 11, 2018 in Marketplace Apps

You + one app + a desert island...

Hi all! My name is Miles and I work on the Marketplace team. We’re looking for better ways to recommend and suggest apps that are truly crowd favorites, so of course we wanted to poll the Community. ...

2,261 views 5 6
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you