Forums

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

append a string to a custom field

Deleted user December 10, 2018

Hello everyone,

I have the following code in a ScriptRunnert Behaviour:

def nif = getFieldByName("NIF")
nif.setHidden(true)

def dni = getFieldByName("DNI")
def number = dni.getValue()
def resto = number % 23

if (resto == 0) {
 nif.setHidden(false)
 def x = dni.getValue().append("T")
 nif.setFormValue(x)
} else if (resto == 1){
 nif.setHidden(false)
 def x = dni.getValue().append("R")
 nif.setFormValue(x)
}

and I get several errors.

The idea is to fill in the custom field "NIF" with the number of the custom field "DNI", and depending on the remainder of the division : number / 23 , to add a letter to it, so that NIF=DNI+Letter

I would be very grateful if anyone could help me, please.

Thanks in advance.

Almu

 

1 answer

1 accepted

1 vote
Answer accepted
Nir Haimov
Community Champion
December 10, 2018

Hi,

Just an idea, maybe "DNI" value returns as a string and than math operations will fail and that is why you get error.

try just to return the DNI value (even in console), if it's a string, than you must parse it to int (number) before doing any math operations.

You may also want to use "string" or "int" instead of "def"

Let me know how you proceed.

Deleted user December 10, 2018

Hi Nir,

Thanks for your help.

Note: DNI is a Number Custom Field

Note: NIF is a Text (single line) Custom Field

Another attempt:

def nif = getFieldByName("NIF")
nif.setHidden(true)

def dni = getFieldByName("DNI")
def number = dni.getValue()
int resto = number % 23

if (resto == 0) {
 nif.setHidden(false)
 def x = (dni.getValue().append("T")).toString()
 nif.setFormValue(x)
} else if (resto == 1){
 nif.setHidden(false)
 def x = (dni.getValue().append("R")).toString()
 nif.setFormValue(x)
}

 

I get the following error in line 6 (int resto): cannot find matching method java.lang.Object#mod(int)

I get the following error in lines 10, 14: cannot find matching method java.lang.Object#append

 

Any idea, please?

Thx

Almu

Deleted user December 10, 2018

Hi again,

I have corrected the error in line 6 with:

def dni = getFieldByName("DNI")
def number = (int) dni.getValue()
def resto = (number % 23).toInteger()

So my current problem is how to add a letter. It seems "append" does not work...

Any suggestion, please?

Cheers,

Almu

Deleted user December 10, 2018

Hi!

Solved!

if (resto == 0) {
 nif.setHidden(false)
 def x = (number + "T").toString()
 nif.setFormValue(x)
}

Cheers,

Almu

Nir Haimov
Community Champion
December 10, 2018

Great!

please mark this answer as solution so it will be closed :)

Deleted user December 10, 2018

Hi again

I realize this is not working

def resto = (number % 23).toInteger()

It does not calculate the remainder.

Does anyone know how to fix it, please?

Thx a lot.

ALmu

Nir Haimov
Community Champion
December 10, 2018

can you write me now your complete code? (after all your changes)

Deleted user December 10, 2018

Sure!

def nif = getFieldByName("NIF")
nif.setHidden(true)

def dni = getFieldByName("DNI")
def number = (int) dni.getValue()
int resto = number % 23

if (resto == 0) {
 nif.setHidden(false)
 def x = (number + "T").toString()
 nif.setFormValue(x)
} else if (resto == 1){
 nif.setHidden(false)
 def x = (number + "R").toString()
 nif.setFormValue(x)
}

Note: number % 23 is not working

Thx!

Nir Haimov
Community Champion
December 10, 2018

This will work for you :)

def nif = getFieldByName("NIF")

nif.setHidden(true)

def dni = getFieldByName("DNI")
def number = dni.getValue()
int resto = Integer.valueOf(number.toString()) % 23

if (resto == 0) {
 nif.setHidden(false)
 def x = (number.toString() + "T").toString()
 nif.setFormValue(x)
} else if (resto == 1){
 nif.setHidden(false)
 def x = (number.toString() + "R").toString()
 nif.setFormValue(x)
} else {
 nif.setHidden(false)
 def x = (number.toString() + "Other Cases").toString()
 nif.setFormValue(x)
}
Nir Haimov
Community Champion
December 10, 2018

You had 2 problems

int resto = Integer.valueOf(number.toString()) % 23

wasn't set right and was failing even that you didn't get error.

And your "if" statement didn't handle cases that not "0" or "1", nothing happens.

Deleted user December 10, 2018

Hi Nir,

You are the best!

Thanks a lot. It is working.

Have a great day :-)

Almu

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events