Foreign Key Constraints in Functions

I've written a stored procedure that performs an insert, and then another insert. See below. The foreign key constraint of the DeclarationBodies table always complains, meaning the declaration_id is not present in the Declarations table... But it should be, given that the select query finds it...