Fix issue with global ID generation

Prior to this fix the temporary ID was being used which was causing
duplicate ID isses in the DB. Now we generate the correct ID once
and use it throughout.