Forums

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

Assign variable into a custom field base on component

Shah Baloch
Contributor
July 27, 2020

There are multiple components in the project, tickets get assign to users base on the component, for example if user select component "Product" then ticket gets assign to product lead, whenever he change status to "Approval Need" it should get assign to a Supervisor. I can add a supervisor in Post function but there are multiple components and there is only one work flow and issue type. If add user there then all ticket assign to him which we don't want, each component has there own lead and supervisor. For example Product component go product team, Customer components ticket assign to Customer team and Supplier to supplier team.

There is two approval Supervisor, I want to create two variable user1 and user2, if user select component "Product" to create a ticket, it should assign user2 otherwise it should add user1  to customer assignee field, then I'll use that assignee field in a User define Post function in workflow, so whenever product lead change the status it should get assign to Product Supervisor, rest of the component it should do nothing or assign to user1. I am trying add following Listener script but it won't working, I know it is not correct, I don't have an idea how to make it work.

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.db.DatabaseUtil
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import java.sql.Timestamp
import com.atlassian.jira.bc.project.component.ProjectComponent

def event = event as IssueEvent
def issue = event.issue
def customFieldObjects = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)
def user1 = 'abc'
def user2 = 'xyz'

def cfAsignee = customFieldObjects.findById("customField_10401")
def components = getFieldById(getFieldChanged()).getValue() as List<ProjectComponent>

if(components == 'Product') {
cfAsignee.modifiedValue(user2)

else

cfAsignee.modifiedValue(user1)

}

Thank you

 

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Nic Brough -Adaptavist-
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 Champions.
August 18, 2021

Rather than try to start debugging this, I'm going to do the consultant thing - take a step back and ask why you are trying to do this the hard way?

A Behaviour will set a field based on user input, and the field it sets will be editable by people, only filled in after people interact with an issue and won't be set if they create or edit in any of the places Behaviours can work (if they use an app or REST call, it won't work)

The data you're trying to get into the fields looks to me to be static calculated data you don't really want the humans to be touching.  So while automating it is a good thing to do, Behaviours is not a good way to do it.

A better way to do it would be with a post-function that calculates the date and puts it into the target fields - this would work on the transitions in your workflow and not need to put anything in front of your users.  It would also work if the transitions are triggered by REST or .  If you wanted to catch Edits, you'd need to do it as a listener instead of a post-function.    And, in both post-functions and listeners, the code would be a lot more simple.

Even better would be a scripted field - it has all the advantages of doing it as a listener, and if you wanted to be really clever with it, you could have it read the issue history for the change of status and calculate from there.  This would enable you to populate the fields for historical issues as well - re-indexing the project after you've got the scripted field working would populate it for all issues, not just ones you create or edit.

RSP
Contributor
August 18, 2021

hi @Nic Brough -Adaptavist- 

I am completely new to this platform and scripting. Will you please tell me what will be the script for post function or listener for above requirement.

RSP
Contributor
August 18, 2021

Hi, 

The "UX status" field is available on transition screen. Depends upon the selection from UX status dropdown status, the other date fields associated with them should get updated.

Nic Brough -Adaptavist-
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 Champions.
August 18, 2021

Ok, that's still an automation you should be doing with a post-function or listener, not a front-end action.

https://library.adaptavist.com/entity/update-the-value-of-custom-fields-through-the-script-console shows you how to work with all types of field in a function script.

TAGS
AUG Leaders

Atlassian Community Events