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,
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
2. I added the sample code to the ScriptRunner console and executed it as shown below:-
3. As expected the Request participants is added as shown below:-
I hope this helps to answer your question. :-)
Thank you and Kind regards,
Ram
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
2. From the Post-Function options, select ScriptRunner's Custom script post-function
3. Add the sample code provided as shown below:-
Below are a couple of test screenshots:-
1. Create a new issue and set the component to SSD
2. Once the Issue is created as expected, the Request Participants are set to Ram Kumar and Max
3. Create a new issue and set the Components to Motherboard.
4. As expected, the Request Participants are set to Ajit and Madona.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I always get confused and not know when to use Behaviour and When Listener. Thank you @Ram Kumar Aravindakshan _Adaptavist_ it worked.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.