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

substract days of custom fields. Scriptrunner

Sergio Palacio April 25, 2018

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

Suggest an answer

Log in or Sign up to answer
0 votes
Sergio Palacio May 4, 2018
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
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 25, 2018

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)
Sergio Palacio April 25, 2018

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)
}
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 25, 2018

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

Sergio Palacio April 26, 2018

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

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 26, 2018

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

TAGS
AUG Leaders

Atlassian Community Events