Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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".

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.


Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase

The benefits of using Jira in different departments

Jira is a great tool to use across different departments. Forget that paperwork – switch to Jira and get that tasks done smoothly. Marketing Jira allows for a complete digital transformation of you...

101 views 0 6
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you