Clearing default text in description field : behaviour plugin

Suhas Patil Community Champion Aug 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

1 answer

1 accepted

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 Patil Community Champion Aug 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.

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 Patil Community Champion Aug 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

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

932 views 5 18
Read article

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot