Script Runner - Add username condition into code

Rob B
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.
February 16, 2018

I have a script that makes a field mandatory in the create transition, but now I need to add another condition into the code so that the field is only mandatory if the username of the creator does NOT contain 'Portal'.

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def CustomFieldValue1 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Retailer ID"));
def CustomFieldValue2 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Retailer ID List"));


if(CustomFieldValue2 == " Please select..."){
invalidInputException = new InvalidInputException("Please Select From Retailer ID List")
}

 Can anyone help?

2 answers

1 accepted

1 vote
Answer accepted
Ivan Tovbin
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.
February 16, 2018

Incidentally, by 'Portal' you mean reporter name or customer request channel type?

Rob B
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.
February 16, 2018

Sorry, yes Portal is just a username

Ivan Tovbin
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.
February 16, 2018

Ok. Try this:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def CustomFieldValue1 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Retailer ID"));
def CustomFieldValue2 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Retailer ID List"));
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

if(CustomFieldValue2 == null && currentUser.getUsername() == "Portal"){
throw new InvalidInputException("Please Select From Retailer ID List")
}
Rob B
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.
February 26, 2018
if(CustomFieldValue2 == null && currentUser.getUsername() != "Portal"){
throw new InvalidInputException("Please Select From Retailer ID List")
}

 "Portal" user can still create the issue without selecting Retailer ID List.

Rob B
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.
February 26, 2018

Ignore me it worked fine! Thanks for your help.

0 votes
Juan Manuel Ibarra
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.
February 16, 2018

Hi,

 

You can use

 

ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().name
Rob B
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.
February 16, 2018

Do i just put that in anywhere? Sorry a bit of a beginner to this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events