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/88b8e5fd257bd6ce08e78230c951f1a2 to your computer and use it in GitHub Desktop.
Save smileham/88b8e5fd257bd6ce08e78230c951f1a2 to your computer and use it in GitHub Desktop.
DescribeSObjects.ajs #jarchi #salesforcecli #salesforce #sfdx
/*
* Describe sObjects in view 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.
*
* This script if a Proof of Concept to populate the detail of selected sObjects created via the GetSObjects script
*
* Once the model has been populated with Salesforce sObjects, create a new view using the sObjects you
* wish further detail on.
*
* Running this script will then:
* * Populate the Description of the sObject elements with the fields of the Salesforce object
* * Create Relationships between the sObjects
* * Create Record Type "data object" and relate these to the Parent via Specialization relationships
*
* Requires jArchi - https://www.archimatetool.com/blog/2018/07/02/jarchi/
* Requires Salesforce CLI
*
* Version 1: Proof of Concept
*
* (c) 2023 Steven Mileham
*
*/
console.clear();
console.log("Describe sObjects in view via Salesforce CLI");
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";
if (!model.findSpecialization("Record Type","data-object")) {
var sObjectSpec = model.createSpecialization("Record Type", "data-object");
}
var theView = $(selection).filter("archimate-diagram-model").first();
console.log(theView);
var theSObjects = $(theView).find("data-object");
var theApplicationFolder = $("folder.Application")[0];
var theParentFolder = $(theApplicationFolder).find("folder."+sfSandbox)[0];
var theSObjectFolder = $(theParentFolder).find("folder.sObjects")[0];
var theRecordTypeFolder = null
if (theParentFolder) {
theRecordTypeFolder = $(theParentFolder).find("folder.Record Types")[0];
}
if (!theRecordTypeFolder) {
theRecordTypeFolder = theParentFolder.createFolder("Record Types");
}
theSObjects.each(function (sObject){
function setChildRelationships(currentValue, index) {
if (!currentValue.deprecatedAndHidden) {
var theTarget = $(theSObjectFolder).find('data-object.'+currentValue.childSObject)[0];
if (theTarget!=null) {
var theRelationship = null;
if (currentValue.relationshipName==null) {
var theRelationships = $(theSource).outRels("composition-relationship");
for (i=0; i<theRelationships.length; i++) {
//console.log("Name:"+theRelationships[i].name);
//console.log("TheTarget:"+theTarget.id+"=="+theRelationships[i].target.id+":"+String(theRelationships[i].target.id==theTarget.id));
if (theRelationships[i].name=="" && theRelationships[i].target.id==theTarget.id) {
theRelationship = theRelationships[i];
break;
}
}
}
else {
theRelationship = $(theSource).outRels("composition-relationship."+currentValue.relationshipName)[0];
}
if (theRelationship==null) {
theRelationship = model.createRelationship("composition-relationship", currentValue.relationshipName, theSource, theTarget);
theRelationship.documentation=currentValue.field
var theTargetVisual = $(theView).find("."+theTarget.name)[0];
//console.log(theTargetVisual+":"+theTarget.id+"=="+sObject.id);
if (theTargetVisual) {
theView.add(theRelationship, theSourceVisual, theTargetVisual);
}
}
}
}
}
function setFields(currentValue,index) {
if (!currentValue.deprecatedAndHidden) {
theSource.documentation = theSource.documentation+=currentValue.label+" ("+currentValue.name+") : "+currentValue.type+"\n";
}
}
// recordTypeInfos
function setRecordTypes(currentValue, index) {
// If active && available && !master
if (currentValue.active && currentValue.available && !currentValue.master) {
theRecordType = model.createElement("data-object", currentValue.name, theRecordTypeFolder);
theRecordType.specialization = "Record Type"
theRecordType.documentation = currentValue.developerName + "\nDefault: "+ currentValue.defaultRecordTypeMapping
theSpecializationRel = model.createRelationship("specialization-relationship", "",theRecordType, theSource);
}
// Put record type elements in Record Types folder.
// Create new record type elements of Specialization Record Type
// Name = Name
// Documentation = developerNames, default,
// Relate new record type element to theSource via Specialization
}
if (sObject.specialization=="sObject") {
var sObjectName=sObject.name;
console.log(sObject);
var theSourceVisual = $(sObject)[0];
var theSource = theSourceVisual.concept;
theSource.documentation="";
console.log(theSourceVisual.labelExpression);
if (theSourceVisual.labelExpression==null || theSourceVisual.labelExpression==""){
theSourceVisual.labelExpression="${name}\n\n${documentation}";
theSourceVisual.textAlignment=TEXT_ALIGNMENT.LEFT;
}
var sfCLIOutput = executeCommand(["C:\\Program Files\\sf\\bin\\sf.cmd","sobject","describe","--sobject", sObjectName, "-o", sfSandbox, "--json"]);
var sObjectDesc = JSON.parse(sfCLIOutput);
console.log (sObject);
sObjectDesc.result.childRelationships.forEach(setChildRelationships);
sObjectDesc.result.fields.forEach(setFields);
sObjectDesc.result.recordTypeInfos.forEach(setRecordTypes);
}
});
//var theSource = $('data-object.'+sObjectName)[0];
console.log("Done");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment