Forums

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

Scriptrunner Behavior script help [populate username when checkbox checked]

Teja
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.
December 22, 2021

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
Mohamed Benziane
Community Champion
December 22, 2021

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

Teja
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.
December 22, 2021

@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

Teja
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.
December 27, 2021

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
atlassian, likes for trees, atlassian community, social impact, tree planting campaign, community kudos, atlassian giving, environmental impact, sustainability, likes for good, atlassian social responsibility, community challenge

Make every click count—help us plant 50,000 trees! 🌳

Want to make your everyday Community actions directly contribute to reforestation? The Atlassian Community can achieve this goal by liking a post, attending an ACE, sending your peers kudos, and so much more!

Help us plant more trees
AUG Leaders

Atlassian Community Events