Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How add users to Request participants field - Jira Service Management?

Shah Baloch April 10, 2023

Hi Community,

I am adding a template to the Jira Service Desk issue description field. It gets populated based on a component selection such as "Purchase". When the user selects the component "Purchase" it'll populate the template in the issue description. The template part is working fine. I want to add a couple of users to the "Request participants" field when the "Purchase" component is selected. I tried adding the Request participants code in the existing template code, I also tried to create a separate script but it's not working, I mean it's not adding users to the field. Any idea why it's not adding users to the field? Here are the two scripts I tried:

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.scriptrunner.db.DatabaseUtil
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def desc = getFieldById("description")
def requestParticipants = getFieldById('customfield_13902')

def purchaseValue = """

* Device Name:
* Device Model:
* Serial#:

""".stripIndent()

def components = getFieldById(getFieldChanged()).value as List<ProjectComponent>
if(components.any{it.name == 'Purchase'}) {
   desc.setFormValue(purchaseValue)
  requestParticipants.setFormValue('user1')

 }

else {
  desc.setFormValue('')
     }

This is the second one I tried

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def requestParticipants = getFieldByName('Request participants')
def components = getFieldById(getFieldChanged()).value as List<ProjectComponent>

if(components.any{it.name == 'Purchase'}){  
   requestParticipants.setFormValue(['user1', 'user2'])      

}

 

Thank you,

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 11, 2023

Hi @Shah Baloch

For your requirement, you can try the sample code below:-

import com.adaptavist.hapi.jira.issues.Issues

def issue = Issues.getByKey('ST-5')

issue.update {
setCustomFieldValue('Request participants', 'ram', 'max')
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

For this code to work, please ensure that you upgrade your ScriptRunner plugin to the latest release, i.e. 8.0.0 so you can use the HAPI feature. 

I am running the sample code using the ScriptRunner console.

 

Below are a couple of test screenshots for your reference:-

1. Below is the example ticket

image1.png

2. I added the sample code to the ScriptRunner console and executed it as shown below:-

image2.png

 

3. As expected the Request participants is added as shown below:-

image3.png

I hope this helps to answer your question. :-)


Thank you and Kind regards,
Ram

Shah Baloch April 11, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ thank you for your reply. I don't want to update the existing tickets. I want to automate the process. Such as if a user creates a ticket and selects the component "Purchase" then it should add those two users into the Request participants field, if the component is not Purchase then it should not add those two users to the field. For the rest of the components, users can add manually if needed but just want to automate one component. Thanks

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 11, 2023

Hi @Shah Baloch

Thank you for your clarification.

If you intend to set the Requested participants field, this can only be done using the ScriptRunncer Console, Post-Function or Listener. Behaviours will not be able to do this.

Below is a working sample code using the Post-Function:-

import com.adaptavist.hapi.jira.issues.Issues
import com.atlassian.jira.bc.project.component.ProjectComponentImpl

def issue2 = Issues.getByKey(issue.key)
def components = issue2.components as List<ProjectComponentImpl>

issue2.update {
if (components.size() > 0 ) {
components.each {
if (it.name == 'SSD') {
setCustomFieldValue('Request participants', 'ram', 'max')
} else if (it.name == 'Motherboard') {
setCustomFieldValue('Request participants', 'madona', 'amitm')
}
}
}
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below are the screenshots of the Post-Function configuration:-

1. Use the create transition and select the Post-Function

post1.png

2. From the Post-Function options, select ScriptRunner's Custom script post-function

post2.png

3. Add the sample code provided as shown below:-

post3.png

 

Below are a couple of test screenshots:-

1. Create a new issue and set the component to SSD

test1.png

2. Once the Issue is created as expected, the Request Participants are set to Ram Kumar and Max

test2.png

3. Create a new issue and set the Components to Motherboard.

test3.png

4. As expected, the Request Participants are set to Ajit and Madona.

test4.png

As mentioned earlier, please ensure that you upgrade your ScriptRunner plugin to the latest release, i.e. 8.0.0 to be able to use the HAPI features demonstrated above.

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

Like Shah Baloch likes this
Shah Baloch April 12, 2023

I always get confused and not know when to use Behaviour and When Listener. Thank you @Ram Kumar Aravindakshan _Adaptavist_ it worked.

TAGS
AUG Leaders

Atlassian Community Events