Hi,
You may want to take a look at the global env variables for the pipeline on your own Jenkins server:
http://JenkinsHost/job/MyJobName/pipeline-syntax/globals
replace the bold part with yours. You may find many things already defined into environment. To access them into groovy script just use the variable directly or using double quote into string:
def myVar = env.BRANCH_NAME
def myVar = "${env.BRANCH_NAME}"
As for the error you are getting, it's the sandbox policies that block the execution for "security" reason.
Either use case:
1- You use the direct pipeline script: You can disable it into the project pipeline config page by unchecking the Use Groovy Sandbox
2- You use scm to fetch the jenkinsfile: You need to go into the Manage Jenkins and then click on In-process Script Approval and approve the wanted function until the script pass for every script possible branches.
Jerome