How map components with description using behaviour script

Shah Baloch July 15, 2020

Hello all, I need some help, here is a script which is mapped with description.

 

def desc = getFieldById("description") 
def customerValue = """\

h2. Please answer the below questions
* Customer Name:
* Customer Id:
* Start Date:
* End Date:
* Additional Information :

""".stripIndent()

if (!desc.formValue) {
desc.setFormValue(customerValue
}

 

The above behaviour script is working fine. I mean whenever users click to create tickets it’ll display those customer questions in the description field. We have multiple components like “Customer, Suppliers, Products”, etc. I would like to have a template for each component, like the above is for the “Customer” component, then I want to create a template for Supplier and Product. That template should be map with components. I mean whenever the user selects Customer it should display the Customer template in the description, if Supplier it should supplier template.

How I can achieve this, I don’t know about the groovy language. I really appreciate your help

1 comment

Comment

Log in or Sign up to comment
Italo Qualisoni [e-Core]
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.
July 15, 2020

You can try something like this:

import com.atlassian.jira.bc.project.component.ProjectComponent

def desc = getFieldById("description")
def components = getFieldById(getFieldChanged()).getValue() as List<ProjectComponent>


def customerValue = """
h2. Please answer the below questions
* Customer Name:
* Customer Id:
* Start Date:
* End Date:
* Additional Information :

""".stripIndent()

def customerValueB = """
h2. Please answer the below questions
* Customer Name:

""".stripIndent()

//multiple components or none
if(components.size() == 0 || components.size() > 1){
return;
}

switch (components.get(0).getName()){
case "Customer":
if (!desc.formValue) {
desc.setFormValue(customerValue)
}
break
case "Supplier and Product":
if (!desc.formValue) {
desc.setFormValue(customerValueB)
}
break
}

 give it a better indentation :)

Like # people like this
Shah Baloch July 16, 2020

Thank you so much @Italo Qualisoni [e-Core]  it is working fine. I made component field mandatory, there is only one issue, like if the user enters any text in the description then select components then it won't display those texts which mapped with the component.  Also if the user selects multiple components then it won't show the second component texts.

 

Is there any way to hide the description unless the user won't pick up a component?

Is there a way to restrict the users to select only one component or display both component texts in the description if they select multiple components?

Is there a way if the user removes the component while creating a ticket, the description text should get removed? suppose user select a component accidentally and then realize it's wrong one and user remove a component, description text should go away?

How can I accomplish all or some of the above mentioned?

 

I really appreciate your help.

Italo Qualisoni [e-Core]
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.
July 16, 2020

Hi @Shah Baloch ,

Great to hear that it worked!

Yes, you can either hide or make the description field readonly until one component has been selected. I think you can play around with both solutions and see what fits best to you.

 

If you want to make the description read only, you can do the following change in the script, adding the bold text inside your if statement and outside as well

...
if(components.size() == 0 || components.size() > 1){
desc.setReadOnly(true)
return;
}
desc.setReadOnly(false)
...

 And to hide the description it's similar, you can use setHidden method:

...
if(components.size() == 0 || components.size() > 1){
desc.setHidden(true)
return;
}
desc.setHidden(false)
...

 

Here you can find all sort of methods that behaviours allow you to use:

https://scriptrunner.adaptavist.com/6.4.0-p5/jira/behaviours-api-quickref.html?utm_source=product-help

I hope this helps you to leverage your business requirement!!

Italo Qualisoni [e-Core]

Like Shah Baloch likes this
Shah Baloch July 17, 2020

@Italo Qualisoni [e-Core]  Thank you so much, Ready only and hidden both options are working fine. 

I tried to go through the link you mention however I couldn't figure out how to clear description texts if I remove component, like Customer component. 

For example, I use hidden option and remove the component, it'll hide the description, when I select another component, it'll still show the description texts of the previous component,  It'll be great if there will be an away to removes a component the description texts also should get remove and when selects another component so it should display the texts for that component. 

I really appreciate your help

Italo Qualisoni [e-Core]
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.
July 17, 2020

I see what you are saying, I think you need to evaluate this behavior because it can be frustrating to the end user that type a description, and then realize that the component is wrong, and then the description will be lost.

My suggestion is to change description only when the field is empty, otherwise you can frustrate your end user :)

This condition is preventing the description be overwritten , it will update the description only if the description form is empty;

if (!desc.formValue) { 
desc.setFormValue(customerValueB)
}

Also I think this code make sense only during the create action, so I would add this in your code:

if (getActionName() != "Create Issue") { 
....your code here.....
}

If this help you please mark this as answered :D

Like Shah Baloch likes this
Shah Baloch July 17, 2020

The code didn't work, maybe I didn't add the code in the right section.

I have a different description for each component, what's why I want to get the text to remove. For example for the "Customer" component, I have only customer-related information, for the "Product" component I only have product-related questions. Because if they accidentally select the Customer component and then realize they suppose to select the product, if the description texts won't go away then they'll add wrong information. 

If description texts get removed by removing a component, that'll be great

Appreciate your help

Italo Qualisoni [e-Core]
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.
July 17, 2020

Try these 2 changes, in the switch condition, remove the if statement:

case "Customer": 
desc.setFormValue(customerValue)
break

 

And to clear the description you can do the following:

//multiple components or none
if(components.size() == 0 || components.size() > 1){
desc.setFormValue('');
return
}
Like Shah Baloch likes this
Shah Baloch July 17, 2020

@Italo Qualisoni [e-Core] Yay, It is working fine. I can't thank you enough. Scriptrunner can be a big help to us. How I can learn this language? I went through the documentation, but there are just some examples and a single line code. Where I should begin. I appreciate your help.

Italo Qualisoni [e-Core]
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.
July 17, 2020

Awesome!

This is a complete app that allow you to customize your environment to meet what you need. 

But I usually like to say to our customers this app must be used with care, because when not used correctly it can impact performance and break the instance when used incorrectly, also make it difficult to migrate to Cloud. 

Don't get me wrong, it unlikely you will break your instance by using Behaviour, but Scriptrunner is much more than that.

The app has scripted fields, listeners, built-in features to help you with some administrative tasks, web fragments, workflow post-functions/condition/validators, rest endpoints , JQL functions to enhance searches...

I recommend you to check theirs documentation that is pretty handful and also take a look in their library that contains some most-used scripts that you can start with: https://library.adaptavist.com/

Also the community is a great place look for help, there are a few members from Adaptavist active in the community.

Other than that you can reach an Atlassian Partner  when you need best practice guidance or training;

Italo Qualisoni [e-Core]

Like Shah Baloch likes this
Shah Baloch July 17, 2020

Thank you so much for your help. I'll check in the test environment first then implement it in production.

I can't see any option to close the conversion or mark it as answered. I got all my answers please close this conversation. 

Thank you so much, you have a great day/night.

Italo Qualisoni [e-Core]
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.
July 17, 2020

Awesome that I could help you!! In the top of this thread you should have a green button to mark this as answered, this will mark this question as closed;

Shah Baloch July 17, 2020

I might not have permission to mark it as closed. I can't see the green button anywhere in the top of the thread. Thank youat_forum.JPG

Italo Qualisoni [e-Core]
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.
July 18, 2020

That's ok, I think some moderator can mark this question as answered. You're welcome!!

Shah Baloch July 27, 2020

We are using a single issuetype and workflow for a project and we have multiple component and component assign to different team. There is an "Assignee" custom field, I would like use it in a workflow post function. I'll define two variables user1 and user2 in the script. 

Is there a way when user select "Customer" component, the CF "Assignee" get a user1 if user select "Product" component, the value of CF "Assignee" should be user2?

I have transition "Approval Needed" Whenever user change the transition  to "Approval Needed" it should use the CF assignee field in the post function. Manually it is working, like whenever I create a ticket I assign a user to the CF Assignee field and whenever user change the transition it'll assign ticket to the user which I added in cf assignee field.

Do I need to user another Behaviour script or I can add it in the same. How I can accomplish this? Appreciate your help.

Italo Qualisoni [e-Core]
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.
July 27, 2020

Hi @Shah Baloch ,

I would recommend you to have a new behavior script for this.

Also I think it would to create a new question since your requirement is different from the initial question;

TAGS
AUG Leaders

Atlassian Community Events