Skip to content

Instantly share code, notes, and snippets.

@shinchit
Last active September 7, 2019 07:31
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 shinchit/d9991feb5e9ee97ee564d13a9f5caa10 to your computer and use it in GitHub Desktop.
Save shinchit/d9991feb5e9ee97ee564d13a9f5caa10 to your computer and use it in GitHub Desktop.
InterlockSelector ( lightning component which is a sObjects selector and is a field selector which interlocks with specified sObject )
<aura:component controller="InterlockSelectorController">
<aura:attribute name="objectName" type="String"/>
<aura:attribute name="fieldName" type="String"/>
<aura:attribute name="required" type="Boolean" default="false"/>
<aura:attribute name="objectOptions" type="List" default="[]"/>
<aura:attribute name="fieldOptions" type="List" default="[]"/>
<aura:handler name="init" value="{!this}" action="{!c.init}" />
<aura:handler name="change" value="{!v.objectName}" action="{!c.changeNameAttribute}"/>
<aura:handler name="change" value="{!v.fieldName}" action="{!c.changeNameAttribute}"/>
<lightning:combobox aura:id="object" name="object" label="target object" value="{!v.objectName}" required="{!v.required}" placeholder="select target object." options="{!v.objectOptions}" onchange="{!c.selectObject}"/>
<aura:renderIf isTrue="{!v.objectName}">
<lightning:combobox aura:id="field" name="field" label="target field" value="{!v.fieldName}" required="{!v.required}" placeholder="select target field" options="{!v.fieldOptions}" onchange="{!c.selectField}"/>
</aura:renderIf>
</aura:component>
public with sharing class InterlockSelectorController {
@AuraEnabled
public static List<Map<String, String>> getObjects() {
List<Map<String, String>> sobjectList = new List<Map<String, String>>();
List<EntityDefinition> entityDefinitions = [SELECT label, MasterLabel, QualifiedApiName FROM EntityDefinition WHERE IsLayoutable = true AND IsQueryable = true Order by MasterLabel];
for (EntityDefinition ed: entityDefinitions) {
Map<String, String> sobjectInfo = new Map<String, String>();
sobjectInfo.put(ed.QualifiedApiName, ed.label);
sobjectList.add(sobjectInfo);
}
return sobjectList;
}
@AuraEnabled
public static Map<String, String> getFields(String objectApiName) {
Map<String, String> retVal = new Map<String, String>();
Map<String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get(objectApiName).getDescribe().fields.getMap();
List<String> fieldNames = new List<String>(fieldMap.keySet());
fieldNames.sort();
for (String fieldName: fieldNames) {
retVal.put(fieldName, fieldMap.get(fieldName).getDescribe().getLabel());
}
return retVal;
}
}
({
init : function(component, event, helper) {
helper.fetchObjectComboboxValue(component);
let changeNameAttribute = component.get('c.changeNameAttribute');
$A.enqueueAction(changeNameAttribute);
},
selectObject: function (component, event, helper) {
let selectedOptionValue = event.getParam("value");
component.set("v.objectName", selectedOptionValue);
if (selectedOptionValue === "") {
component.set("v.fieldName", "");
}
helper.fetchFieldComboboxValue(component, selectedOptionValue);
},
selectField: function (component, event, helper) {
let selectedOptionValue = event.getParam("value");
component.set("v.fieldName", selectedOptionValue);
},
changeNameAttribute : function(component, event, helper) {
component.find("object").set("v.value", component.get("v.objectName"));
helper.fetchFieldComboboxValue(component, component.get("v.objectName"));
component.find("field").set("v.value", component.get("v.fieldName"));
}
})
({
fetchObjectComboboxValue : function(component) {
let action = component.get("c.getObjects");
let opts = [];
action.setCallback(this, function(response) {
if (response.getState() == "SUCCESS") {
let values = response.getReturnValue();
if (values != undefined && values.length > 0) {
opts.push({
label: "--- None ---",
value: ""
});
}
for (let i = 0; i < values.length; i++) {
Object.keys(values[i]).forEach(function(key) {
let value = values[i][key];
opts.push({
label: value,
value: key
});
});
}
component.set("v.objectOptions", opts);
}
});
$A.enqueueAction(action);
},
fetchFieldComboboxValue : function(component, objectApiName) {
let action = component.get("c.getFields");
action.setParams({
"objectApiName": objectApiName
});
let opts = [];
action.setCallback(this, function(response) {
if (response.getState() == "SUCCESS") {
var fieldMap = response.getReturnValue();
for (var k in fieldMap) {
opts.push({ value:k, label:fieldMap[k]});
}
component.set("v.fieldOptions", opts);
}
});
$A.enqueueAction(action);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment