You should be okay to test for and setblock with no problem, provided you use the correct syntax and different scoreboard objectives. I find it interesting though, you never actually change the value of the "chesthasitem" objective (Unless you are using something behind the scenes, like a stats command); obviously it would never delete the block because there is no player that has a score of "chesthasitem" that is at least 1. a series of /testforblock commands to determine if the block near a player has an item in it. Keep in mind, if you want to test for the item in any inventory, you are going to have to test not only chests, but every other block that has an inventory as well. I can work on a function that could do this for you, but I suggest other simpler solutions if possible. Could I have a bit more information about what you are doing this on? Here are some questions that may help me determine another option if possible.
1. Are you doing this on an adventure map? If so, can your players place down any blocks that have inventory?
2. Is this a survival world?
3. Do you just want the function?