I faced a similar error while executing script, a quick solution to this just add this to end of your function:
after $$ signs and before ';'
Like this:
enter image description here