If you are referring to the calibre template language, you can't do what you are asking. A template cannot modify book metadata. All variables are local, meaning they disappear when the template finishes.
The
audit log plugin might give you what you want.