Tell them as soon as you know you have to leave and allow room for discussion later. Make sure to have a courteous tone, and give them as much as information possible. Assure them that you will cover up all the pending work.