We are using Tridion 2011 SP1. When a component is being published we are updating a metadata field of component in our component template. 

The code which updates the component metadata field is working fine but we found the updated values are not in the version of the component getting published. Rather, values from the previous version of component are getting published. 

When we open the component in the Tridion GUI, the component shows the values have been updated by the component template.

What changes we have to make to ensure the publishing code is working with the latest (updated by component template) version of component?