When post is Published, insert into custom table a number – custom post type

Take a look at WordPress’ Post Status Transitions hooks: https://codex.wordpress.org/Post_Status_Transitions

If you’ve added proper debugging (so that you die at the top of your hooked function) it sounds like you’re not hooking into the right action.

Since you’re adding some meta and checking for it to see if you should run again, you could hook into publish_custom-post-type. Also, the status may be confusing. I see you’re trying draft, but that does not include new or auto-draft. Make sure you are confident about the previous status and the new status as well as the post_type.

error code: 523