groovy script error in Jira

Hi,

l need your help regarding groovy script statement.

l had a mistake in the following statement:

Timestamp lastClosedDate = issue.getCustomFieldValue(lastClosedDateCf)

After equals, statement gives an error.

Thanks a lot.

 

1 answer

0 vote
Alexey Matveev Community Champion Dec 24, 2017

Hello,

Could you provide the whole script?

import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tamamlanmaTarihiCf = customFieldManager.getCustomFieldObject(111111)
Timestamp tamamlanmaTarihi = issue.getCustomFieldValue(tamamlanmaTarihiCf)
def lastClosedDateCf = customFieldManager.getCustomFieldObject(222222)
Timestamp lastClosedDate = issue.getCustomFieldValue(lastClosedDateCf)

String statusId = issue.getStatusObject().getSimpleStatus().getId()
String bulguDurumu = ''

if(issue.getProjectId().toString() == '123444' && issue.getIssueTypeId() == '1234'){

if(statusId == '10'){

if(lastClosedDate.compareTo(tamamlanmaTarihi) > 0){

bulguDurumu = 'xxxxx'

}
else{

bulguDurumu = 'yyyyyy'

}

}
else{

Timestamp now = new Timestamp(Calendar.getInstance().getTimeInMillis())

if(tamamlanmaTarihi){

if(now.compareTo(tamamlanmaTarihi) > 0){

bulguDurumu = 'aaaaaaaa'

}
else{

bulguDurumu = 'bbbbbbbb'

}
}
else{

bulguDurumu = 'ccccccccc'

}
}

}

return bulguDurumu

Alexey Matveev Community Champion Dec 24, 2017

You need to convert Object to Timestamp. And you incorrectly initialize custom fields. It should be like this

import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tamamlanmaTarihiCf = customFieldManager.getCustomFieldObjectByName("fieldName")
Timestamp tamamlanmaTarihi = (Timestamp) issue.getCustomFieldValue(tamamlanmaTarihiCf)
def lastClosedDateCf = customFieldManager.getCustomFieldObjectByName("fieldName")
Timestamp lastClosedDate = (Timestamp) issue.getCustomFieldValue(lastClosedDateCf)

String statusId = issue.getStatusObject().getSimpleStatus().getId()
String bulguDurumu = ''

if(issue.getProjectId().toString() == '123444' && issue.getIssueTypeId() == '1234'){

if(statusId == '10'){

if(lastClosedDate.compareTo(tamamlanmaTarihi) > 0){

bulguDurumu = 'xxxxx'

}
else{

bulguDurumu = 'yyyyyy'

}

}
else{

Timestamp now = new Timestamp(Calendar.getInstance().getTimeInMillis())

if(tamamlanmaTarihi){

if(now.compareTo(tamamlanmaTarihi) > 0){

bulguDurumu = 'aaaaaaaa'

}
else{

bulguDurumu = 'bbbbbbbb'

}
}
else{

bulguDurumu = 'ccccccccc'

}
}

}

return bulguDurumu
Alexey Matveev Community Champion Dec 24, 2017

If you use 13921L then you should use customFieldManager.getCustomFieldObject(13921L). But if you have serveral environments for Jira (dev, test, prod ) then it is better to reference your field by Name

customFieldManager.getCustomFieldObjectByName("fieldName"). And if you use getCustomFieldObjectByName it must be the field name not the field Id. Because in each of the environments there is a chance that your field will have different ids.

Isn't getCustomFieldObjectByName deprecated? What would be the best solution moving forward if so?

Alexey Matveev Community Champion Dec 29, 2017

You can use getCustomObjectsByName. But you would need to iterate over the List.

Because of the strange decision to have multiple fields with the same name, we need to work with a List.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

Meet the AUG leaders of Northern Virginia

@Rachel Wright (Jira Genie), @Billy Poggi (AUG NOVA, DC), and @Dana Jansen (Confluence Queen) are just some of the folks that lead one of the world's most active Atlassian User Group (AUG)....

121 views 4 9
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you