Ok. Thanks to your suggestions i've finalized the script (which is about 211 lines long.
For the brave ones who dare to read it i post it here.
function countLetters(myString : text,myLetter : text) do
length(replacex(myString, "[^" + myLetter + "]", "g", ""))
end;
let xPCog := replace(Cognome, " ", "");
let xConCog := replacex(upper(xPCog), "A|E|I|O|U", "g", "");
let xVocCog := replacex(upper(xPCog), "B|C|D|F|G|H|J|K|L|M|N|P|Q|R|S|T|V|W|X|Y|Z", "g", "");
let xCog := xConCog + xVocCog;
let xLunCog := length(xCog);
let xPNom := replace(Nome, " ", "");
let xConNom := replacex(upper(xPNom), "A|E|I|O|U", "g", "");
let xVocNom := replacex(upper(xPNom), "B|C|D|F|G|H|J|K|L|M|N|P|Q|R|S|T|V|W|X|Y|Z", "g", "");
let xNom := xConNom + xVocNom;
let xLunNom := length(xNom);
let cfparz := switch xLunCog do
case 1:
text(xConCog) + "XX"
case 2:
text(xConCog) + "X"
case 3:
text(xConCog)
default:
substring(text(xCog), 0, 3)
end + switch xLunNom do
case 1:
text(xConNom) + "XX"
case 2:
text(xConNom) + "X"
case 3:
text(xNom)
default:
if length(text(xConNom)) > 3 then
substring(text(xNom), 0, 1) + substring(text(xNom), 2, 4)
else
substring(text(xNom), 0, 3)
end
end + substring(text(year(DataNascita)), 2, 4) + switch text(month(DataNascita)) do
case "1":
"A"
case "2":
"B"
case "3":
"C"
case "4":
"D"
case "5":
"E"
case "6":
"H"
case "7":
"L"
case "8":
"M"
case "9":
"P"
case "10":
"R"
case "11":
"S"
case "12":
"T"
default:
""
end + switch Sesso do
case 1:
if length(text(day(DataNascita))) = 1 then
"0" + text(day(DataNascita))
else
text(day(DataNascita))
end
case 2:
text(day(DataNascita) + 40)
default:
""
end + (
let xComune := text(ComuneNascita);
(select Comuni where Comune = xComune).CodiceComune
);
let xCarDis := substr(cfparz, 0, 1) + substr(cfparz, 2, 1) + substr(cfparz, 4, 1) + substr(cfparz, 6, 1) + substr(cfparz, 8, 1) + substr(cfparz, 10, 1) + substr(cfparz, 12, 1) + substr(cfparz, 14, 1);
let xCarPar := substr(cfparz, 1, 1) + substr(cfparz, 3, 1) + substr(cfparz, 5, 1) + substr(cfparz, 7, 1) + substr(cfparz, 9, 1) + substr(cfparz, 11, 1) + substr(cfparz, 13, 1);
let a := countLetters(xCarPar, "A");
let b := countLetters(xCarPar, "B");
let c := countLetters(xCarPar, "C");
let d := countLetters(xCarPar, "D");
let e := countLetters(xCarPar, "E");
let f := countLetters(xCarPar, "F");
let g := countLetters(xCarPar, "G");
let h := countLetters(xCarPar, "H");
let i := countLetters(xCarPar, "I");
let j := countLetters(xCarPar, "J");
let k := countLetters(xCarPar, "K");
let l := countLetters(xCarPar, "L");
let m := countLetters(xCarPar, "M");
let n := countLetters(xCarPar, "N");
let o := countLetters(xCarPar, "O");
let p := countLetters(xCarPar, "P");
let q := countLetters(xCarPar, "Q");
let r := countLetters(xCarPar, "R");
let s := countLetters(xCarPar, "S");
let t := countLetters(xCarPar, "T");
let u := countLetters(xCarPar, "U");
let v := countLetters(xCarPar, "V");
let w := countLetters(xCarPar, "W");
let x := countLetters(xCarPar, "X");
let y := countLetters(xCarPar, "Y");
let z := countLetters(xCarPar, "Z");
let x0 := countLetters(xCarPar, "0");
let x1 := countLetters(xCarPar, "1");
let x2 := countLetters(xCarPar, "2");
let x3 := countLetters(xCarPar, "3");
let x4 := countLetters(xCarPar, "4");
let x5 := countLetters(xCarPar, "5");
let x6 := countLetters(xCarPar, "6");
let x7 := countLetters(xCarPar, "7");
let x8 := countLetters(xCarPar, "8");
let x9 := countLetters(xCarPar, "9");
let sumP := a * 0 + b * 1 + c * 2 + d * 3 + e * 4 + f * 5 + g * 6 + h * 7 + i * 8 + j * 9 + k * 10 + l * 11 + m * 12 + n * 13 + o * 14 + p * 15 + q * 16 + r * 17 + s * 18 + t * 19 + u * 20 + v * 21 + w * 22 + x * 23 + y * 24 + z * 25 + x0 * 0 + x1 * 1 + x2 * 2 + x3 * 3 + x4 * 4 + x5 * 5 + x6 * 6 + x7 * 7 + x8 * 8 + x9 * 9;
let aa := countLetters(xCarDis, "A");
let bb := countLetters(xCarDis, "B");
let cc := countLetters(xCarDis, "C");
let dd := countLetters(xCarDis, "D");
let ee := countLetters(xCarDis, "E");
let ff := countLetters(xCarDis, "F");
let gg := countLetters(xCarDis, "G");
let hh := countLetters(xCarDis, "H");
let ii := countLetters(xCarDis, "I");
let jj := countLetters(xCarDis, "J");
let kk := countLetters(xCarDis, "K");
let ll := countLetters(xCarDis, "L");
let mm := countLetters(xCarDis, "M");
let nn := countLetters(xCarDis, "N");
let oo := countLetters(xCarDis, "O");
let pp := countLetters(xCarDis, "P");
let qq := countLetters(xCarDis, "Q");
let rr := countLetters(xCarDis, "R");
let ss := countLetters(xCarDis, "S");
let tt := countLetters(xCarDis, "T");
let uu := countLetters(xCarDis, "U");
let vv := countLetters(xCarDis, "V");
let ww := countLetters(xCarDis, "W");
let xx := countLetters(xCarDis, "X");
let yy := countLetters(xCarDis, "Y");
let zz := countLetters(xCarDis, "Z");
let xx0 := countLetters(xCarDis, "0");
let xx1 := countLetters(xCarDis, "1");
let xx2 := countLetters(xCarDis, "2");
let xx3 := countLetters(xCarDis, "3");
let xx4 := countLetters(xCarDis, "4");
let xx5 := countLetters(xCarDis, "5");
let xx6 := countLetters(xCarDis, "6");
let xx7 := countLetters(xCarDis, "7");
let xx8 := countLetters(xCarDis, "8");
let xx9 := countLetters(xCarDis, "9");
let sumD := aa * 1 + bb * 0 + cc * 5 + dd * 7 + ee * 9 + ff * 13 + gg * 15 + hh * 17 + ii * 19 + jj * 21 + kk * 2 + ll * 4 + mm * 18 + nn * 20 + oo * 11 + pp * 3 + qq * 6 + rr * 8 + ss * 12 + tt * 14 + uu * 16 + vv * 10 + ww * 22 + xx * 25 + yy * 24 + zz * 23 + xx0 * 1 + xx1 * 0 + xx2 * 5 + xx3 * 7 + xx4 * 9 + xx5 * 13 + xx6 * 15 + xx7 * 17 + xx8 * 19 + xx9 * 21;
let resto := ((sumP + sumD) / 26 - floor((sumP + sumD) / 26)) * 26;
let finalC := switch text(round(resto)) do
case 0:
"A"
case 1:
"B"
case 2:
"C"
case 3:
"D"
case 4:
"E"
case 5:
"F"
case 6:
"G"
case 7:
"H"
case 8:
"I"
case 9:
"J"
case 10:
"K"
case 11:
"L"
case 12:
"M"
case 13:
"N"
case 14:
"O"
case 15:
"P"
case 16:
"Q"
case 17:
"R"
case 18:
"S"
case 19:
"T"
case 20:
"U"
case 21:
"V"
case 22:
"W"
case 23:
"X"
case 24:
"Y"
case 25:
"Z"
default:
""
end;
cfparz + finalC