Skip to content

Instantly share code, notes, and snippets.

@westc
Last active February 9, 2022 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westc/d0240a1295d4df88d1e1c53bf911b241 to your computer and use it in GitHub Desktop.
Save westc/d0240a1295d4df88d1e1c53bf911b241 to your computer and use it in GitHub Desktop.
Convert a JSONifiable value into valid Apex (Salesforce) using apexify() in JavaScript.
var apexify;
(function() {
apexify = function(value, indent) {
indent = indent || 0;
let indentation = 'string' === typeof indent
? indent
: new Array(indent + 1).join(' ');
let newLine = indentation ? '\n' : '';
let allSpacing = indentation ? newLine + indentation : '';
let space = indentation ? ' ' : '';
let arrow = space + '=>' + space;
let comma = ',' + newLine;
return recurseApexify(
JSON.parse(JSON.stringify(value)),
indentation,
newLine,
allSpacing,
arrow,
comma
);
};
function recurseApexify(value, indentation, newLine, allSpacing, arrow, comma) {
var typeName = Object.prototype.toString.call(value).slice(8, -1);
if (value === null || typeName === 'Number' || typeName === 'Boolean') {
return JSON.stringify(value);
}
if (typeName === 'Array') {
let hasData = value.length > 0;
let myNewLine = hasData ? newLine : '';
return 'new List<Object>{'
+ myNewLine
+ value
.map(function(x) {
return recurseApexify(x, indentation, newLine, allSpacing, arrow, comma);
})
.join(comma)
.replace(/^/gm, indentation)
+ myNewLine
+ '}';
}
if (typeName === 'String') {
return "'"
+ value.replace(/'/g, "\\'").replace(/[^\x20-z]/g, substringify)
+ "'";
}
let keys = Object.keys(value);
let hasData = keys.length > 0;
let myNewLine = hasData ? newLine : '';
return 'new Map<String,Object>{'
+ myNewLine
+ keys
.map(function(key) { return recurseApexify(key) + arrow + recurseApexify(value[key], indentation, newLine, allSpacing, arrow, comma); })
.join(comma)
.replace(/^/gm, hasData ? indentation : '')
+ myNewLine
+ '}';
}
function substringify(x) {
return JSON.stringify(x).slice(1, -1);
}
})();
function apexifyInIO(value, tabSize) {
return apexify(JSON.parse(value), tabSize);
}
// HIDE \\
console.load('local://apexify.js');
// \\
var data = {"success": false, "error": "Authentication failed.", "u":{value:null}, user:{first:'Chris',last:'West',grades:[1,2,3,4,5],details:'sdf\n\t\rxcvxc'}};
// Normal call without indents
console.log(apexify(data));
// \\
// Call with indenting (number of spaces)
console.log(apexify(data, 3));
// \\
// Call with indenting (specify single spacing)
console.log(apexify(data, '\t'));
function getPageParts(params) {
return {
title: "Apexify - Convert JSON To Apex",
description: "Easily conversion any JSON into Salesforce Apex notation.",
buttons: { submit: 'Convert', clear: false }
};
}
function getInputs(params) {
var inputs = params.inputs;
function onInput(value) {
try {
JSON.parse(value);
}
catch (e) {
this.reportError('The specified JSON is invalid.');
}
}
return [
{
key: 'json',
label: 'JSON',
type: 'code',
lang: 'json',
value: inputs.json && inputs.json.value,
onInit: onInput,
onInput: onInput
}
];
}
function getOutputs(params) {
var inputs = params.inputs;
var apexCode = apexify(params.inputs.json, 2);
return [
{
label: 'Salesforce Apex',
type: 'code',
lang: 'apex',
value: apexCode
},
{
label: 'Download Apex Code',
type: 'download-button',
value: apexCode,
downloadName: 'example.apex'
}
];
}
eval(GIST_FILES['apexify.js'].text);
{
// Title of the IO App
"title": "apexify()",
// Name of the function responsable for transforming the input into the output.
"transform": "apexifyInIO",
// Where to find the code that will be eval'd.
"files": ["apexify.js", "~apexifyInIO.js"],
// Input setup
"input": { "language": "json" },
// Output setup
"output": { "language": "apex" },
// Parameters that can be set.
"params": [
{
"type": "number",
"label": "Indentation (Spaces)",
"name": "tabSize",
"value": 4
}
],
// Type of YourJS IO-App
"type": "instant"
}

JavaScript Snippet - apexify()

You can use this simple function to turn JSON into Apex equivalent.

console.load('local://~example.js');
@westc
Copy link
Author

westc commented Jun 24, 2021

@westc
Copy link
Author

westc commented Feb 7, 2022

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