I see You test vfilenotesbyclaimid.doctype for "FILENOTE" and "LETTER" but they have different length (If they are not varchar of course) are Yoy sure Your function run this clone of DO CASE?
Also is all values of doctype are in uppercase?
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.