Skip to main content
Example scripts
arrow icon
To homepage
Jira Service Desk
Data centre icon
Data Center

Copy SLA information to a Custom Field

Created 1 year ago, Updated 21 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira Service Desk (3.10 - 3.16)
compatibility bullet
ScriptRunner For Jira (5.6.14)
compatibility bullet
Jira Service Desk (4.0 - 4.6)
compatibility bullet
ScriptRunner For Jira (5.6.14)
Language |
groovy
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.servicedesk.api.sla.info.SlaInformation
import com.atlassian.servicedesk.api.sla.info.SlaInformationService
import com.atlassian.servicedesk.api.util.paging.PagedResponse
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.atlassian.servicedesk")
@PluginModule SlaInformationService slaInformationService

// SLA field name
final slaName = '<SLA name>'

// Gets the SLA information querying SLA service for the current issue
def query = slaInformationService.newInfoQueryBuilder()
    .issue(issue.id)
    .build()
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def slaFormatter = slaInformationService.durationFormatter
def slaPagedResponse = slaInformationService.getInfo(user, query).right().get() as PagedResponse
def sla = slaPagedResponse.results.find { (it as SlaInformation).name == slaName } as SlaInformation

if (sla?.ongoingCycle?.present) {
    // If there is an ongoing SLA. it takes the current ongoing SLA remaining time and format it as duration of "X hours Y minutes"
    log.error("SLA remaining time: ${sla.ongoingCycle.get().remainingTime}")
    slaFormatter.format(user, sla.ongoingCycle.get().remainingTime)
} else {
    // If there is no ongoing SLA, it takes last completed cycle remaining type and format it as duration of "X hours Y minutes"
    log.error("SLA remaining time: ${sla?.completedCycles?.last()?.remainingTime}")
    slaFormatter.format(user, sla?.completedCycles?.last()?.remainingTime)
}
Having an issue with this script?
Report it here