Inside foreach, replace data.name with entity.name. Also, wrap the code with trycatch.
data.name
entity.name