Referencing a Date Picker field in post function script runner

Hi,

I'm trying to create a issue linked to another based on a transition.
I would like the summary of the new ticket be as follows:"CustomFieldFromParent CustomText ParentSummary"

I have managed to get it working except for the customfield from the parent.
I have had no issue in trying to reference a text custom field, the script I used for this can be found here:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue; 
 
 
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
  
CustomField CustomFieldName = customFieldManager.getCustomFieldObjectByName( "CustomFieldName" );
 
 
issue.summary = issue.getCustomFieldValue( CustomFieldName) + ' txt ' +issue.getSummary();

However for some reason I can't seem to reference a Date Picker type customfield.
Is there something I would need to change in this script?

Also the custom field I'm trying to reference has spaces in the name, does that complicate things?

Thanks in advance,

3 answers

I think you should convert the date time field value to string

The following should work

issue.summary = issue.getCustomFieldValue(CustomFieldName).toString() + ' txt ' +issue.getSummary();

Hi,

I did some additional testing and included your transform to string.
What I noticed was that If I use customfields which have spaces in the name such as "Custom Field" it gives error regardless of the customfield type.

I tried using the following, can you see if i made some mistake in the way I reference to the customfield?

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue; 
  
  
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
   
CustomField 'Custom Field' = customFieldManager.getCustomFieldObjectByName( "Custom Field" );
  
issue.summary = issue.getCustomFieldValue('Custom Field').toString() + ' txt ' +issue.getSummary();



To be honest i do not know if spaces in name creates this type of error. But, in order to avoid this is better to get your custofields with the id. The code should be like

CustomField customField_test = customFieldManager.getCustomFieldObject( customField_test id );

That way you have a better control in your references and you don't have to change your script in case you rename the custom field.

Hope that helps.

> CustomField 'Custom Field' = customFieldManager.getCustomFieldObjectByName( "Custom Field" );

This is not valid... you are trying to use 'Cust Field' as a variable name.
Just do:
def customField = customFieldManager.getCustomFieldObjectByName( "Custom Field" );
Add some log.warn (...) lines and check the log.
Doing a quick groovy tutorial or two wouldn't do you any harm at this point...

I only have access to the JIRA administration in which I'm using a test project and am testing via Script Post-Function on transitions and checking the result if I execute the transition.

The only testing tool I have is GroovyConsole in which I can't reference to JIRA to check what exactly is going wrong, only if my logic seems correct..

def date = new Date()
def formdate = date.format("yyyy-MM-dd");
println formdate

The above seems to work just fine so I'm unsure of what is going wrong.

As such I'm trying to solve the issue using your vast knowledge of issues like this.
Please find the update below:

I have been trying to fix the issues I'm having but no luck so far.
I have managed to eliminate the first part of the issue only to walk straight into the next one.
This is the current situation:

CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();

def variable1 = customFieldManager.getCustomFieldObjectByName( "customfield" );

def variable1 = variable2;

issue.summary = "[" + issue.getCustomFieldValue(variable2).toString() + "]" + " txt " + issue.getSummary();

output:
summary of the cloned issue is:" [2014-04-10 00:00:00.0] txt OriginalSummary"


close to the goal now... only thing we need to do is remove the 0's from the summary..

I tried the following but it doesn't proccess:

CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();

def variable1 = customFieldManager.getCustomFieldObjectByName( "customfield" );

def variable1 = variable2.format('yyyy-MM-dd');

issue.summary = "[" + issue.getCustomFieldValue(variable2).toString() + "]" + " txt " + issue.getSummary();

output:
issue isn't even created.

I feel like I'm missing something simple, please advise.

I think the following should work

def cf_2 = customFieldManager.getCustomFieldObjectByName('Starting Date')

val_2 = issue.getCustomFieldValue(cf_2).toString().split(" ")[0]

issue.setSummary(val_2 + issue.getSummary())

In the end the main issue was trying to use [ in my custom text.
I'm not succesfully using the code below:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue; 

CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();

def cf = customFieldManager.getCustomFieldObjectByName('CustomField')
val = issue.getCustomFieldValue(cf).toString().split(" ")[0]

issue.setSummary(val + ' Custom text ' + issue.getSummary())



Thanks for the tips.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Amir Kazemi
Posted an hour ago in Jira

We want to know what Jira Service Desk apps you're using!

Hi Community! My name is Amir and I’m on the Jira Service Desk product marketing team at Atlassian. Our team would love to understand how you’re leveraging our ecosystem for Jira Service Desk. Wha...

9 views 0 2
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