Referencing a Date Picker field in post function script runner

Remon Vrij April 1, 2014

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

0 votes
Remon Vrij July 6, 2014

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.

0 votes
Tsol
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 7, 2014

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())

0 votes
Tsol
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 1, 2014

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();

Remon Vrij April 2, 2014

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();



Tsol
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 2, 2014

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.

JamieA
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 2, 2014

> 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...
Remon Vrij April 7, 2014

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.

Suggest an answer

Log in or Sign up to answer