Note that this script only works in Scripting Extension for a single record. If you want to run this in an automation, you will need to make several changes, including how you get the triggering record and using a substitute for the btoa function, which is not available in automation scripts.