Fixed onBeforeWrite function. Previous query was returning null.