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, mindful member, mindful member badge, atlassian community, community kudos, community giveaway, atlassian swag, community values, empathy and kindness, badge challenge, atlassian learning, community engagement, swag giveaway

Earn a Badge, Win a Prize 🎁

Earning the Mindful Member badge proves you know how to lead with kindness, plus it enters you into a giveaway for exclusive Atlassian swag. Take the quiz, grab the badge, and comment on our announcement article to spread the good vibes!

Start here
AUG Leaders

Atlassian Community Events