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

Custom field not update in post function

Ivan Martin Herrero March 27, 2017

Hi,

I am using Scriptrunner in a post function to set a value of a custom field, but it is not working. I have the post function the first in the list of post functions, but it still not working. There is my code:

 

 

import java.time.LocalDateTime
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.datetime.*
import org.joda.time.DateTime;
import java.time.ZoneId
import java.sql.Timestamp
import java.util.Calendar
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.MutableIssue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def textCf = customFieldManager.getCustomFieldObjectByName("Fecha Finalización SLA")
def tgtField = customFieldManager.getCustomFieldObjectByName("Móvil - Descripción Estado")
LocalDateTime defaultTime = LocalDateTime.now().withHour(22).withMinute(00)
def hour = LocalDateTime.now().getHour()
LocalDateTime horaLimite20 = LocalDateTime.now().withHour(20).withMinute(00)
LocalDateTime horaLimite14hoy = LocalDateTime.now().withHour(14).withMinute(00)
LocalDateTime horaLimite14manana = LocalDateTime.now().withHour(14).withMinute(00).plusDays(1)
Date fechaDef = new DateTime(Date.from(defaultTime.atZone(ZoneId.systemDefault()).toInstant())).toDate()
Date fecha20 = new DateTime(Date.from(horaLimite20.atZone(ZoneId.systemDefault()).toInstant())).toDate()
Date fecha14 = new DateTime(Date.from(horaLimite14hoy.atZone(ZoneId.systemDefault()).toInstant())).toDate()
Date fecha14manana = new DateTime(Date.from(horaLimite14manana.atZone(ZoneId.systemDefault()).toInstant())).toDate()
MutableIssue issueToUpdate = (MutableIssue) issue;

if(tgtField.getValue(issue).toString().equalsIgnoreCase( "Grabación manual solicitud Siebel")) 
if(hour > 14 && hour < 20)
issueToUpdate.setCustomFieldValue(textCf,new Timestamp(fecha20.time) ) 

else if (hour >= 20 && hour <24)
issueToUpdate.setCustomFieldValue(textCf,new Timestamp(fecha14manana.time) )

else
issueToUpdate.setCustomFieldValue(textCf,new Timestamp(fecha14.time) ) 

else if(tgtField.getValue(issue).toString().equalsIgnoreCase( "Verificación manual estado provisión Siebel") ||
tgtField.getValue(issue).toString().equalsIgnoreCase( "Grabación manual pedido en SAP") )
if(hour > 14 && hour <=24)
issueToUpdate.setCustomFieldValue(textCf,new Timestamp(fecha14manana.time) )

else
issueToUpdate.setCustomFieldValue(textCf,new Timestamp(fecha14.time) )

 

But if i write the line: 

issueToUpdate.setCustomFieldValue(textCf,new Timestamp(fecha14.time))

before the ifs, it changes the value. So i think the problem is when the instruction is inside the if, but i dont get how to solve it 

 

Thanks in advance

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

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.
March 27, 2017

I'm not sure it's seeing "hour" as a simple number object.  Are you sure it contains a number (and that it's the right number)?

Ivan Martin Herrero March 27, 2017

Yes, it is, there is no problem in the conditions of the If clauses. The flow goes ahead them correctly and enters in the right way.

The problems is the instruction:

issueToUpdate.setCustomFieldValue(textCf,new Timestamp(fecha14.time))

inside the clauses seems not doing anything, but the flow of the script goes through it, i checked it with logs

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.
March 27, 2017

But you said it works outside the if statement?  Just being inside an "if" won't change what it does, which implies it's the "if" statement that isn't working.

Ivan Martin Herrero March 27, 2017

Yes, it works outside.

That is what i ask here, because i tested that it enters inside the if, but the instruction don't work. The if statement is working correctly

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.
March 27, 2017

Wrapping a line in an "if" block does not change the way the line works.

There is one nagging doubt, but it's hard to explain - what, exactly is the if/then/else block that is failing?

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.
March 28, 2017

Agree with Nic... but do yourself a favour and use braces, ie if () { ... }

It's too easy to make a mistake otherwise.

TAGS
AUG Leaders

Atlassian Community Events