Skip to content

Instantly share code, notes, and snippets.

@smileham
Last active November 28, 2023 15:58
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 smileham/79b4b250d8737c688f325934e6797ca1 to your computer and use it in GitHub Desktop.
Save smileham/79b4b250d8737c688f325934e6797ca1 to your computer and use it in GitHub Desktop.
GetSObjects.ajs #jarchi #salesforcecli #salesforce #sfdx
/*
* Get sObjects via Salesforce CLI
*
* Please ensure you have first authenticated against your Salesforce sandbox and have defined an Alias
* No Warranty is provided for this script.
*
* Script calls the Salesfoce CLI for a given alias and retrieves all sObject records, these are created
* as "Data Objects", set with a specialization of sObject and stored in a folder.
*
* A second script can then populate the detail of selected sObjects (DescribeSObjects)
*
* Requires jArchi - https://www.archimatetool.com/blog/2018/07/02/jarchi/
* Requires Salesforce CLI
*
* Version 1.1: Updated to put namespaced objects into folders.
* Version 1: Proof of Concept
*
* (c) 2023 Steven Mileham
*
*/
console.clear();
console.log("Get sObjects via Salesforce CLI - POC");
function executeCommand(commandArray) {
var runtime = Java.type("java.lang.Runtime").getRuntime();
var Scanner = Java.type("java.util.Scanner");
var process = runtime.exec(commandArray);
var inputStream = process.getInputStream();
var scan = new Scanner(inputStream).useDelimiter("\\A");
var val = "";
if (scan.hasNext()) {
val = scan.next();
}
else {
val ="";
}
return val;
}
var sfSandbox = "SANDBOX ALIAS"
//TODO fixme!
if (!model.findSpecialization("sObject","data-object")) {
var sObjectSpec = model.createSpecialization("sObject", "data-object");
}
var theGroup = $("grouping."+sfSandbox+" sObjects")[0];
var theApplicationFolder = $("folder.Application")[0];
var theParentFolder = $(theApplicationFolder).find("folder."+sfSandbox)[0];
console.log(theParentFolder);
if (theParentFolder) {
var theSObjectFolder = $(theParentFolder).find("folder.sObjects")[0];
}
if (!theGroup) {
theParentFolder = theApplicationFolder.createFolder(sfSandbox);
theSObjectFolder = theParentFolder.createFolder("sObjects");
theGroup = model.createElement("grouping",sfSandbox+" sObjects");
}
var ignoreCustom = false;
var ignoreHistory = true;
var ignoreChange = true;
var ignoreShare = true;
var ignoreMDT = true;
var ignoreFeed = true;
console.log(theSObjectFolder);
//var args = Array.prototype.slice.call("C:\\Program Files\\sf\\bin\\sf.cmd","sobject","list","--sobject", "all", "-o", "srmdevdx");
var sfCLIOutput = executeCommand(["C:\\Program Files\\sf\\bin\\sf.cmd","sobject","list","--sobject", "all", "-o", sfSandbox, "--json"]);
var sObjects = JSON.parse(sfCLIOutput);
sObjects.result.forEach(createDataObject);
function createDataObject(currentValue, index) {
var theSObject = $(theSObjectFolder).find("data-object."+currentValue)[0];
//console.log(theFolder+":"+theSObject);
//console.log("Checking:"+currentValue);
if (!theSObject) {
var ignore = false;
if (ignoreCustom && currentValue.endsWith("__c")) {
ignore = true;
}
if (ignoreFeed && currentValue.endsWith("__Feed")) {
ignore = true;
}
else if (ignoreHistory && currentValue.endsWith("History")) {
ignore = true;
}
else if (ignoreChange && currentValue.endsWith("ChangeEvent")){
ignore = true;
}
else if (ignoreMDT && currentValue.endsWith("__mdt")) {
ignore = true;
}
else if (ignoreMDT && currentValue.endsWith("Share")) {
ignore = true;
}
//console.log(index+":"+currentValue+":"+ignore);
if (!ignore) {
var namespace = currentValue.match("^([a-zA-Z_0-9]+)__.+__(c|Feed|r|c_hd|Share|History|ChangeEvent|mdt)$");
if (namespace!=null && namespace[1]!=null) {
var namespaceFolder = $(theSObjectFolder).find("folder."+namespace[1])[0];
if (!namespaceFolder) {
namespaceFolder = theSObjectFolder.createFolder(namespace[1]);
}
thisFolder = namespaceFolder;
}
else {
thisFolder = theSObjectFolder;
}
//console.log(currentValue+":"+thisFolder+":"+namespace[1]);
var sObject = model.createElement("data-object", currentValue, thisFolder);
sObject.specialization = "sObject";
var theRelationship = model.createRelationship("composition-relationship","",theGroup, sObject)
}
}
}
console.log("Complete");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment