How to set the summary of an issue with groovy script at "Create Transition"?

Hey there!

I have problem with my script. The point is when an issue created, the summary of the issue should begins with the value of the "Customer" custom field. My script works in all transitions, but the create...

I've tried several ways.
I started with transientVars["issue"].summary. It based on Jamie's create sub-task script, wich I use at the create transition. I fill in the "Additional issue actions" field with the following:

issue.summary = "Some text - " + transientVars["issue"].summary;

And it works fine.

I've tried with issue changeholder too without any result (maybe it's my fault, I'm not a real coder).

My script wich works is the next (but not at create):

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption

//Issue issue  = (Issue) transientVars.get(issue);

Issue issue  = issue;

String FIELD_NAME = "Customer";

String SUMMARY_VALUE = issue.summary;
 
ComponentManager componentManager = ComponentManager.getInstance();

CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();

CustomField customField = customFieldManager.getCustomFieldObjectByName(FIELD_NAME);


if (customField != null) {

 LazyLoadedOption option = customField.getValue(issue);

        if (option != null){

       String CUSTOMER = option;

       String newSummary = CUSTOMER+" - "+SUMMARY_VALUE;

       if (!SUMMARY_VALUE.contains(CUSTOMER)){

transientVars["issue"].summary = CUSTOMER+" - "+SUMMARY_VALUE;

//transientVars.get("issue").setSummary(newSummary);

//issue.summary = newSummary;

}

else{

return false;

}

            }

        else{

return false;

        }        

}

else {

return false;

     }

The first post function is "Creates the issue originally." in the workflow.

Any idea, comment or help are appreciated.

1 answer

1 accepted

1 votes
Henning Tietgens Community Champion May 06, 2013

If the issue is already created and you change the issue object after that, you have to update the issue manually.

issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Did you try to do the changes before the issue is created (without the above mentioned update)?

Henning

Henning Tietgens Community Champion May 06, 2013

You can get the user like this

componentManager.jiraAuthenticationContext.getLoggedInUser()

Hi Henning,

Thank you for your comments. I tried to run the script befor the issue is created. It doesn't works like the update. I'm affraid I don't have the issue while the create transition is over. Do you have any idea how to get the issue object a different way?

Henning Tietgens Community Champion May 06, 2013

I'm using a script before the issue is created (without update) and I'm simply use "issue.assignee" for reading the value und for writing a new value.

Where I'm not sure is about reading a customfield value before the issue is created. You should try using issue as the object to operate on, so try

customer = issue.getCustomFieldValue(customField)?.getValue()

to read the value of the option and use this change the summary

issue.summary = customer + " - " + issue.summary

If this doesn't work you should use log.error() to log some debug information to see which variable isn't filled like we think.

Henning

You're right!

I tried this:

import com.atlassian.jira.issue.Issue

Issue issue = issue;
issue.summary = "Test text" + issue.summary;

It works, and now my script runs befor the creation without update. There must be something wrong with the value of the custom field.

Thank you Henning!

If someone is interested, the final script is the next:

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.ComponentManager

Issue issue = issue;

String FIELD_NAME = "Customer";

ComponentManager componentManager = ComponentManager.getInstance();

CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();

CustomField customField = customFieldManager.getCustomFieldObjectByName(FIELD_NAME);

customer = issue.getCustomFieldValue(customField)?.getValue();

if (customer && issue.summary){

if (!issue.summary.contains(customer)){

issue.summary = customer + " - " + issue.summary;

}

else{

return false;

}

}

else{

return false;

}

Yes, bingo... subtle difference between issue.getCustomFieldValue(customfield) and customField.getValue(issue)

Hi, Working on Jira v.6.3.9, there are some changes to do on the code above (eg. using now ComponentAccessor). - Is there a way to automatically fill the Summary without the reporter who created the issue has to enter anything ? On my tries, when he clicks on "Create" button, the empty mandatory summary must be filled before the script in post function begins. Best regards Robert

Henning Tietgens Community Champion Dec 11, 2014

You have to do this via JavaScript.

Hi, Thank you, but difficult. I added this javascript below in the field configuration description, setting a default fixed value, but hidding the summary field on the form: <script type="text/javascript"> var summaryField=AJS.$("#summary").parent(); AJS.$("#summary").val("<Default will be auto-changed>"); summaryField.hide(); </script> Then, on validators, I verified to be sure all needed values will be filled/selected before the creation of the issue Then, after create, on post function, I took what I need and replace default summary value by the good built one. Best regards

Suggest an answer

Log in or Register to answer

Stay in touch

Be the first to know what's trending on Atlassian Community