User Defined Script for Sales Order

I am calculating a UDF (by script) on each SO Line.  I need a script that will accumulate those Line amounts and save it to a header field.  I'm hoping some smart guys knows how to do this.