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
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi!
Solved!
if (resto == 0) {
nif.setHidden(false)
def x = (number + "T").toString()
nif.setFormValue(x)
}
Cheers,
Almu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great!
please mark this answer as solution so it will be closed :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
can you write me now your complete code? (after all your changes)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Nir,
You are the best!
Thanks a lot. It is working.
Have a great day :-)
Almu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.