Forums

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

Condition on custom numeric field which is empty

Deepali Bagul
Contributor
June 5, 2018

Hi All,

I have behavior set for a custom numeric field named "Scalar". I want to set an initializer on edit screen for this field. If the field exisitng value is empty, set default to '0' or else set the existing value on the FormField for scalar field. Below is the code I am testing

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.user.ApplicationUser

def cd = getFieldByName("Scalar")
//def cd = customFieldManager.getCustomFieldObject(getFieldChanged())


def svalue = cd.getValue()

log.error("value: " +svalue)

def defaultValue = 0

if (getActionName() == null) {
          if(svalue == null) {
                    cd.setFormValue(defaultValue)
            }else{
                   cd.getFormValue()
                    }
}

I get an error "java.lang.NumberFormatException: For input string: "" "

How do I handle this, please guide.

Thanks,

Deepali 

 

1 answer

1 accepted

1 vote
Answer accepted
Marcos Sanchez
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.
June 6, 2018

Hi Deeepali,

 

I've been reviewing and testing your script for a while and it seems to work fine.

The only thing that I can tell to you is that if the script that you pasted is the full script, it could be simple.

def cd = getFieldByName("Scalar")

def value = cd.getValue() ? cd.getValue() : 0

if (getActionName() == null){
cd.setFormValue(value)
}

 

Hope that helps and it works for you.

 

Regards,
Marcos

Deepali Bagul
Contributor
June 6, 2018

Hello @Marcos Sanchez

Thanks for your prompt reply. Let me explain you the use case,

I have one ticket whose scalar is set to 50 and Another ticket whose scalar value is empty/unset.

So what I am trying on edit screen is when the scalar unset, on the edit screen it should set the value to '0' but scalar is set to 50 than edit screen it should show 50 and not overwrite the value to default 0. Hope this makes sense.

With the above script it does set the default value on edit screen (this should happen only when the value is empty/null), but with above code it also overwrites the existing Scalar value (for example, it overwrites the 50 value to 0)

Thanks in advance for all your help,

Marcos Sanchez
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.
June 6, 2018

Hi,

 

It was my fault, I'm sorry, I've missed to say that the script doesn't have to be an initializer.

Put the script on the field as the screenshot shows and tell me how it goes.

2018-06-06_16h35_40.png

You have the list to add the field on the bottom of the screen.

 

Regards,
Marcos

Deepali Bagul
Contributor
June 6, 2018

Hello @Marcos Sanchez,

Thankyou so much, it worked with the behavior :). I am also additional conditions on the behavior but it seems be causing an issue. I am also restricting the field name 'scalar' to writable only if current user is the listed project manager on ticket.

It works for when scalar is set to 0 but when it is set to 50 on edit screen it is still writable even though current user doesn't matches the PM.

Below my full script 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.user.ApplicationUser

def cd = getFieldByName("Scalar Priority")
//def cd = customFieldManager.getCustomFieldObject(getFieldChanged())
def value = cd.getValue() ? cd.getvalue() : 0
//def cs = 1..100
log.error("scalar value for this issue is " + value)


def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def pm = getFieldByName("Project Manager");
def pmVal = pm.getValue()
log.error("pmValue: " + pmVal)


if (pmVal != currentUser.getUsername()) {
cd.clearError()
// cd.setHidden(true)
cd.setReadOnly(true)
} else {
// cd.setError("You cannot edit this field")
cd.setReadOnly(false)
// cd.setRequired(true)
}


if (getActionName() == null) {
log.error("The string is null or empty " + value)
cd.setFormValue(value)
}

//if (cd.getValue().toString().length() < 3) {
if (value.toInteger() >= 1 && value.toInteger() <= 100) {
// next line makes no sense, because 'value' is read from this field
//cd.setFormValue(value)
// cd.setRequired(false);

cd.clearError();
} else {
// cd.setRequired(true);
cd.setError("range is 1-100")
}

Thanks in advance,

Regards,

Deepali Bagul 

Marcos Sanchez
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.
June 6, 2018

Hi,

Maybe it could be because this is not correct:

def value = cd.getValue() ? cd.getvalue() : 0

I explain myself:

The first cd.getValue() is correct, but the second one isn't.

With groovy there are two options for this: cd.getValue() or cd.value and you have did a mix of them writing cd.getvalue() and this way won't work.

Please verify and change that and if it stills failing I will check the rest of the code, but at first view it looks that it can be the problem...

It would be awesome if you could also check the logs, because most of this issues are in logs and you could solve them by yourself.

Regards,
Marcos

Deepali Bagul
Contributor
June 6, 2018

Thankyou @Marcos Sanchez,

It worked :). You made my day!

I change the second part to cd.getValue(). I missed that error.

Thanks,

Regards,

Deepali Bagul

Marcos Sanchez
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.
June 6, 2018

Hi,

It's great to hear that works!

Have a nice day!

 

Best regards

Marcos

Suggest an answer

Log in or Sign up to answer