Hi Victor,
You don't have to use a UDF to get pieces or KGs. There are fields for that in SAP B1. It's usually not ideal to add UDFs unless really necessary because as you can imagine it's adding more data for the system to process.
In this case instead of using T1.Quantity you could just use T1.Weight1 (provided your items are set up with weights of course).
Just a thought. Maybe you have your reasons
Cheers.