Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,467,457
Community Members
 
Community Events
177
Community Groups

substract days of custom fields. Scriptrunner

HI i have 2 fields.  one is filled in the creation from.
but the other, need to filled by a condition.  

the due date need to have a working/business day. (not saturday and not sunday) then, for example If my hiring date is monday. I need to substract the days until the last working/business day (friday).

import java.sql.Timestamp
import com.atlassian.jira.issue.fields.CustomField

def cf_duedate = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Due Date'}
Timestamp HiringDate = (Timestamp) getCustomFieldValue("Hiring Date")

if (HiringDate [Calendar.DAY_OF_WEEK] == Calendar.SATURDAY){
HiringDate = HiringDate - 1
issue.setCustomFieldValue(cf_duedate, HiringDate)
}

if (HiringDate [Calendar.DAY_OF_WEEK] == Calendar.SUNDAY){
HiringDate = HiringDate - 2
issue.setCustomFieldValue(cf_duedate, HiringDate)
}

 

in the log I'm see an error message

2018-04-25 11:35:41,159 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: POPA-3, actionId: 1, file: null
groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getCustomFieldValue() is applicable for argument types: (java.lang.String) values: [Hiring Date]
	at Script1035.run(Script1035.groovy:5)






2 answers

import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.MutableIssue
import java.sql.Timestamp
import java.util.Calendar

IssueManager issueManager = ComponentAccessor.getIssueManager();
MutableIssue issue = issueManager.getIssueByCurrentKey("POPA-29")
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField cf_HiringDate = customFieldManager.getCustomFieldObjectByName("Hiring Date");
CustomField cf_duedate = customFieldManager.getCustomFieldObjectByName("Due Date");
ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
Date hiringDate = (Date) issue.getCustomFieldValue(cf_HiringDate)


Calendar calendarHiringDate = Calendar.getInstance();
calendarHiringDate.setTime ( hiringDate ); // convert your date to Calendar object
int daysToDecrement; //

if (calendarHiringDate.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY){ //

daysToDecrement = -3;

}else if (calendarHiringDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){ //

daysToDecrement = -2;

}else if (calendarHiringDate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){ //

daysToDecrement = -1;

}

calendarHiringDate.add(Calendar.DATE, daysToDecrement); //
issue.setCustomFieldValue(cf_duedate, new Timestamp(calendarHiringDate.getTimeInMillis())); //
log.warn("DUE DATE: " + issue.getCustomFieldValue(cf_duedate))

issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, true); //


I changed my code logic 

all is ok. buy the due date field is not updated.


using the log line I can get the value but the issue is not updated.

DUE DATE: 2018-05-04 00:00:00.0

I suspect the problem maybe is the format date. my field is only date, not date/time.

Any idea? Can I format the date value before fill the cf_duedate???

I'm using mm/dd/yyyy.


0 votes

getCustomFieldValue requires the issue and the custom field, not just the name of it.

Try

def cf_HiringDate = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Hiring Date'}
Timestamp HiringDate = (Timestamp) issue.getCustomFieldValue(cf_HiringDate)

Thanks Nic for your reply.

New error message

2018-04-25 13:38:14,456 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: POPA-4, actionId: 1, file: null
groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getCustomFieldValue() is applicable for argument types: (null) values: [null]
 at Script1046.run(Script1046.groovy:5)

Follow my change.

import java.sql.Timestamp
import com.atlassian.jira.issue.fields.CustomField
def cf_HiringDate = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Hiring Date'}
def cf_duedate = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Due Date'}
Timestamp HiringDate = (Timestamp) getCustomFieldValue(cf_HiringDate)

if (HiringDate [Calendar.DAY_OF_WEEK] == Calendar.SATURDAY){
HiringDate = HiringDate - 1
issue.setCustomFieldValue(cf_duedate, HiringDate)
}

if (HiringDate [Calendar.DAY_OF_WEEK] == Calendar.SUNDAY){
HiringDate = HiringDate - 2
issue.setCustomFieldValue(cf_duedate, HiringDate)
}

It is not finding a custom field with that name now.  Is it configured to for that project and issue type?

The field exist and are configured for my project.
 I'm trying to do it in a post-function on the creation transition.

Captura.PNG

Nevertheless, it's not finding the field for the issue.

Oh, hang on, this bites me every time.  "string" == "string" usually returns false because Java is doing an object comparison, rather than content string evaluation.

Try "Hiring Date".equals(it.name) instead of it.name == "Hiring Date".

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events