Substring error

good morning, sorry for my english. I have a problem with a debug error. It always appears to me

"TypeError: Cannot read property 'substring' of undefined"

the function I have

var codice = msg.payload.rfin;
msg.payload = codice;
msg.payload = msg.payload.substring(12,msg.payload.length - 0);

if (msg.payload === "F5582B") {
var test = true ;
msg.payload = test;
return msg;
}else {
return [ null, msg ];
}

I thank you for help

The error is saying that at that point msg.payload has the value undefined, which means that codice is undefined so msg.payload.rfin was undefined.
Feed the message you are passing to the function into a debug node and see what it says.