Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner Behavior script help [populate username when checkbox checked]

Hi Folks,

I have been trying to populate the current username for the user picker field upon checking the checkbox as Approved, when uncheck Approved clear username.

RCT Late Approval [] Approved (Checkbox)

RCT Late Approver (Single user picker)

tried below code did not work 

//Populate username//
import groovy.transform.BaseScript
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.component.ComponentAccessor

@BaseScript FieldBehaviours fieldBehaviours

def rctLate = getFieldByName("RCT Late Approval")
def rctLateApproval = rctLate.getValue() as String

if (rctLateApproval == "Approved")
{

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def funcCompleteApprover = getFieldById("customfield_13606")  //customfield_13606 RCT Late Approver
funcCompleteApprover.setFormValue(currentUser.username)

}

Need help.

Regards
T

1 answer

0 votes

Hi @Teja 

What error do you have ?

Can you try this

funcCompleteApprover.setFormValue(currentUser.getUserame())

instead of

funcCompleteApprover.setFormValue(currentUser.username)

https://docs.atlassian.com/software/jira/docs/api/7.0.6/com/atlassian/jira/user/ApplicationUser.html

@Mohamed Benziane 

I don't see any logs error using above script.

When I change `username` to `getUsername()`

I see error like this

User1.PNG

Infact, If I use only script without checkbox if condtion the user population works fine

import groovy.transform.BaseScript
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.component.ComponentAccessor

@BaseScript FieldBehaviours fieldBehaviours


def rctLate = getFieldByName("RCT Late Approval")
def rctLateApproval = rctLate.getValue() as String

//if (rctLateApproval == "Approved"){


def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def funcCompleteApprover = getFieldById("customfield_13606")
funcCompleteApprover.setFormValue(currentUser.username)

User2.PNG

Note: Script that I using in the Initialiser

Regards
Teja

Hi @Mohamed Benziane 

Tweak in the if condition works now

import groovy.transform.BaseScript
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.component.ComponentAccessor

@BaseScript FieldBehaviours fieldBehaviours

def rctLate = getFieldByName("RCT Late Approval")
def rctLateApproval = rctLate.getValue() as String
def funcCompleteApprover = getFieldById("customfield_13606") 

if (rctLateApproval?.contains ("Approved")){
    def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
    //customfield_13606 RCT Late Approver
    funcCompleteApprover.setFormValue(currentUser.username)
}else{
    funcCompleteApprover.setFormValue("")
}

Thanks

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Software

👋 Looking for 15-20 volunteers to test Atlassian training content

Hi everyone! Are you interested in beta testing Atlassian University’s newest (unreleased!) training course? We’re looking for 15-20 volunteers to test our newest training course, Basic reporting...

767 views 24 28
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you