Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner Behavior - If status any of [X, Y, Z], make read-only and display error

Lacey Also September 30, 2020

Trying to pick ScriptRunner back up for the organization and I'm running into some problems. Trying to read the issue status and make a field read-only... but I want to tell users why it's read-only, hence using a script rather than the OOTB read-only marker.

I've read almost every community article that mentions this, and even did a direct copy/paste from https://scriptrunner.adaptavist.com/6.9.1/jira/recipes/behaviours/scripted-conditions.html ... It either enforces ALL the time or NONE of the time. The same is true of the in-line edit... It's always locked, no matter what group I put in there.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.config.StatusManager
StatusManager statusManager = ComponentAccessor.getComponentOfType(StatusManager.class)

// Get the current user
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// Get the changed field
def exampleField = getFieldById(getFieldChanged())


if (ComponentAccessor.getGroupManager().getGroupsForUser(currentUser)?.find { it.name == "UI-fragment-users" }) {
exampleField.setAllowInlineEdit(true)
} else {
exampleField.setAllowInlineEdit(false)
}

if (statusManager.getStatuses()*.getName() != ["To Do", "Analysis", "Intake Agenda", "Intake Meeting", "Prioritization"]) {
exampleField.setError("To maintain data integrity, this field is locked for edits after Prioritization.")
exampleField.setReadOnly(true)
} else {
exampleField.clearError()
exampleField.setReadOnly(false)
}

 

I've also tried 

issue.getStatus().getName()

and

underlyingIssue.getStatus().name

 

But I can't seem to get it to work.

1 answer

2 votes
Matthew Clark
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.
October 3, 2020

Hi Lacey

If you are using

def exampleField = getFieldById(getFieldChanged())

then you most likely have this script applied to a field directly within the behaviour configuration.

What field is this "exampleField"?


Generally, the status of an issue does not change while in the form window that behaviours work on, so it does not really make sense to have this script on a field and it should really be in the behaviour initialiser script area.


You could try a script like this as an initialiser.

def myField = getFieldByName("TextFieldB")

if(!underlyingIssue){
// if there is no underlyingIssue then you are on the create screen and there is nothing to get the status fro,
return
}

def currentStatus = underlyingIssue.status.name
def testStatusList = ["In Progress", "Done"]

if(currentStatus in testStatusList){
log.warn("status ${currentStatus} found in ${testStatusList}")
myField.setReadOnly(true)
myField.setDescription("To maintain data integrity, this field is locked for edits after Prioritization.")
}else{
myField.setReadOnly(false)
myField.setDescription("")
}


Regards

Matthew

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events