Skip to content

Instantly share code, notes, and snippets.

Last active November 19, 2022 20:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save westc/1db1f19c0b9e676fd1638ddad9c93e99 to your computer and use it in GitHub Desktop.
getApexString() - Takes a string and returns the corresponding Apex (Salesforce) string. If string ends up being too long for one line it will be shown as a multiple line expression using String.join().
function getApexString(string, maxOneLinerLength=80) {
var normal = "'" + JSON.stringify(string).slice(1, -1).replace(/\\"/g, '"').replace(/'/g, "\\'") + "'";
return (normal.length > maxOneLinerLength && string.indexOf('\n') >= 0)
? "String.join(new String[] {\n\t" + normal.replace(/\\[^n]|(\\n)/g, (m, isNewline) => isNewline ? "',\n\t'" : m) + "\n}, '\\n')"
: normal;
// Where the JS code lies
"files": ["getApexString.js"],
// Name of the function responsable for transforming the input into the output.
"transform": "getApexString",
// Arguments that will be passed to the function by name.
"params": [
"name": "maxOneLinerLength",
"label": "Maximum Length of a One-liner",
"type": "number",
"min": 3,
"step": 1,
"value": 80
// Input setup
"input": { "language": "text" },
// Output setup
"output": { "language": "apex" },
// Type of YourJS IO-App
"type": "split"
Copy link

westc commented Feb 14, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment