Copy Value from cascading list to custom field

Arpit Doshi April 17, 2014

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

0 votes
Answer accepted
Arpit Doshi April 23, 2014

Resolved using

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

0 votes
Boris Georgiev _Appfire_
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.
April 17, 2014

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")
}

Arpit Doshi April 20, 2014

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.

Arpit Doshi April 20, 2014

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")

Boris Georgiev _Appfire_
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.
April 20, 2014

Okay try using :

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

Boris Georgiev _Appfire_
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.
April 20, 2014

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

Arpit Doshi April 20, 2014

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.

Boris Georgiev _Appfire_
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.
April 20, 2014

Have you checked if cfBusiness variable is not null ?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events