How can I check a field and insert a value to another field with conditions?