Clearing default text in description field : behaviour plugin

Suhas P
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.
August 29, 2016

I have created a behaviour using behaviour plugin for populating default text in Description field while creating an issue. Mapping is for a Project say "Project A" and issue type "Change Request".

Behaviour works fine for correct selection of the Project and Issue Type. However to make it more appropriate, the default text should get clear on selection of the different issue type which does not happen with the current script.  Can anyone help me with achieving this behaviour?

Following is the script i am using in the Initialiser.

def desc = getFieldById("description")
   
def defaultValue = """Example Text """
if (getActionName() == "Create"){
      desc.setFormValue(defaultValue)
}
else {
    
    }

 

Thank in Advance.

Cheers

5 answers

1 accepted

3 votes
Answer accepted
Jonny Carter
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.
August 29, 2016

Instead of putting the script in the initializer, I'd put it in a server-side script on the Issue Type field. To remove your added text whenever someone switches to/from the issue type, the best thing to do would be to get the currently input value and just use a .replaceAll to remove the example text.

You can limit this behaviour to a particular project when you setup the mapping.

Also, as a side note, I thought I'd make it so the default value gets prepended to the description. That way, if they've already entered something when they switch between issue types, their other text doesn't get overwritten.

def desc = getFieldById("description")
def issueType = getFieldById("issuetype")
    
def defaultValue = """Example Text """
def currentValue = desc.getFormValue()
/*
Replace 100001 with your issue type's ID; 
Alternatively, you can use the IssueTypeManager class to get the ID 
based on the Issue Type's name and other properties for clarity, and to 
harden the script against Issue Type changes later
*/
if (issueType.value == "100001"){ 
      desc.setFormValue("$defaultValue - $currentValue")
}
else {
     desc.setFormValue(desc.value.toString().replaceAll("Example Text ", ""))
}

This does have the drawback that someone might type in "Example Text" themselves and see it removed. That said, it will preserve anything they put into the description field that wasn't the phrase "Example Text".

Note: I'm assuming that you have updated to a version of ScriptRunner that includes Behaviours as a feature, and aren't using an old version of the separate Behaviours Plugin.

Suhas P
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.
August 29, 2016

Thanks for the prompt response. Yes, I am using Script Runner which has Behaviour as feature.

I think I have not clarified the requirement properly. Sorry for the confusion.

Above suggested script by you replaces the "Example Text" on the action other than "Create", but i do not want that. I want to clear the default text i.e. "Example Text" on 'Create' action only when I change to different Issue Type than Change Request on the same pop-up screen. I have mappings for a project and Issue Type Change Request.

Jonny Carter
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.
August 30, 2016

That makes more sense... I just sort of assumed the "if" condition was the one you wanted, and didn't look too closely! smile

I've updated my answer.

Suhas P
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.
August 31, 2016

I found my mistake that in the mapping section i was mapping with Project and only one issue type"Change Request" which was making not to trigger script when there is a selection of different issue type. I made the changes and script works fine.

Somehow desc.setFormValue(desc.value.toString().replaceAll("Example Text ", ""))didnt work for me.. but the basic functionality was enough for me for now so will investigate why it is not working for me. If you could guess any errors, please let me know.

Thanks Jonny for your help. Much appreciated. Have great time smile

Sanket Mane April 12, 2018

@Suhas P What changes did you made here?

Currently, I am also facing the same problem and mapped it to my Project and a particular issue type. Why is it wrong?

Suhas P
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 18, 2018

@Sanket Mane you should add additional condition for issue type along with getActionName() == "Create"  as below. and I use this code in initialiser.

In mapping use a project and All Issue Types (not a specific issue type), then the code will run for every issue type selection and set or clear the description based on the issue type selection.

def desc = getFieldById("description")

def defaultValue = """Example Text """
if ((getActionName() == "Create") && (issueContext.issueType.name =="Task")){
desc.setFormValue(defaultValue)
}
else {
desc.setFormValue("")
}

Beware that clear field will clear the description field completely . So you can use it or you can also combine it with the login given by Jonny above in the answer with replaceAll to clear only default text and keep the other text. Depends on you how you want.

Sanket Mane April 18, 2018

@Suhas P

This seems to have helped but caused another issue. Due to the implementation of this behaviour- whenever we try to edit the description field- it doesnt show the required content inside it. I see all the content on the View Screen but when I click on the edit screen Description field appears to be empty.

Can you help me on it. 
Thank you 

Suhas P
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 19, 2018

Ohh 

Suhas P
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 19, 2018

@Sanket ManeOhh I think its because of the Else statement. Modify the Else statement like this 

def desc = getFieldById("description")

def defaultValue = """Example Text """
if ((getActionName() == "Create") && (issueContext.issueType.name =="Task")){
desc.setFormValue(defaultValue)
}
else ((getActionName() == "Create") && (issueContext.issueType.name !="Task")){
desc.setFormValue("")
}
else {
}

 

Like Dave Liao likes this
Deleted user June 25, 2018

I have the same problem. The "description"-field has two display mode: Text and visual.

If you stay in the "Text"-Mode, then the field gets updated correctly, but if i am in the "Visual"-Mode then the description field gets not updated.

Do you have an idea how to force the update of the visual mode?

Like Christophe Paquignon likes this
Paresh Nawale November 23, 2019

@[deleted] did you find solution for your problem? I am facing the same issue when in visual mode behaviour code is not firing.

1 vote
Darío Díez August 12, 2019

Hi everyone,

I have the same problem, Description field doesn't clean up when the conditions of the behaviour are not fullfilled.

I have tried all suggestions which are in this thread, but none works. Could you please help me to solve it?

Thanks  a lot in advance.

Darío Díez August 13, 2019

I have been able to fix it.

I have mapped the project with all issue types and I have add in the script the following conditional in order to remove the description field:

if (issueContext.issueType.name == "Bug") { 
desc.setFormValue(defaultValue)
}else {
desc.setFormValue("")
}

Once applied it, when I choose one of my projects and select an issue type different of Bug, my description field is cleaned up.

I hope it helps.

KR.

Like # people like this
Yoav Ashkenazi August 27, 2019

Sir, you just made my day!

Shereen May 7, 2021

Works like a charm! Thank you!!

0 votes
Edimara Souza March 4, 2021

I have a questions about it:

 

I use this script on my jira and it works very well!
But, when I will go to edit some issues all the information in the description field desapear:
How can I solve this?epictemplate.PNG

Suhas P
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 21, 2021

You should use getActionName() == "Create" in the condition so script won't clear the description on Edit action.

Like # people like this
0 votes
Arun Sharma August 13, 2019

Ok, my requirement was :-

I need to set a template description for a specific Project and Issue type, and if a user chooses a different issue type, the description should remain blank.

so here's what worked for me (you can easily tweak this for your requirement)

 

def desc = getFieldById("description")

def defaultValue = """
* *LINE 1*
**  line 1.1

* *LINE 2*
**  line 2.1

 """

def des = desc.getValue();
if ((getActionName() == "Create") && (issueContext.issueType.name =="Story") &&
!desc.getValue().toString().contains("LINE 1") &&
!desc.getValue().toString().contains("LINE 2")){

desc.setFormValue(defaultValue)
}
desc =getFieldById("description")
des=desc.getValue();


if(issueContext.issueType.name !="Story" &&
desc.getValue().toString().contains("LINE 1") &&
desc.getValue().toString().contains("LINE 2")){
desc.setFormValue(" ");
}

And need to map it to *All fields*

0 votes
Arun Sharma August 13, 2019

Got similar issue, will revert back if I found anything

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events