If you are upgrading the building to the next level,
but ended up in the situation when you need a resource from this building:
 
you can utilize rollback feature.
 
In your upgrade window, press on red recycle button it will give back 100% of your resources and get your building back to the operating state.
 
After that produce the amount of items you need for an upgrade and boost your building back up :)
 
Also, to prevent this type of situation in the future you can look at the information window before upgrading the building.