Clearing default text in description field : behaviour plugin

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

1 answer

1 accepted

This widget could not be displayed.

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.

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.

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.

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

@Suhas Patil [Riada] 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?

@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.

@Suhas Patil [Riada]

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 

@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 {
}

 

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?

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

165 views 1 3
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you