Get Current User Id in Lightning
You need to call the $A.get("$SObjectType.CurrentUser.Id")
, to get the User Id in the Lightning JS Controller.
var userId = $A.get("$SObjectType.CurrentUser.Id"); Console.log(userId);
You need to call the $A.get("$SObjectType.CurrentUser.Id")
, to get the User Id in the Lightning JS Controller.
var userId = $A.get("$SObjectType.CurrentUser.Id"); Console.log(userId);
To reference a specific resource in component markup, we can use $Resource.resourceName
within an expression. resourceName
is the Name of the static resource. In a managed packaged, the resource name must include the package namespace prefix, like $Resource.yourNamespace__resourceName
.
Sample Code:
<aura:component> <!-- Stand-alone static resources image file--> <img src="{!$Resource.resourceName}"/> <!-- Stand-alone static resources image file with Namespace--> <img src="{!$Resource.yourNamespace__resourceName}"/> <!-- Asset from an archive static resource image file--> <img src="{!$Resource.resourceName + '/assets/images/logo.png'}"/> <!-- Asset from an archive static resource image file with Namespace--> <img src="{!$Resource.yourNamespace__resourceName + '/assets/images/logo.png'}"/> </aura:component>
We can write SOQL query on AuraDefinitionBundle
object to get all Lightning Component Bundles from an Org.
Sample SOQL:
SELECT ApiVersion,CreatedDate,DeveloperName,MasterLabel,NamespacePrefix FROM AuraDefinitionBundle
A Lightning component bundle includes following resources:
Resource | Resource Name | Usage |
---|---|---|
Component or Application | sample.cmp or sample.app |
The only required resource in a bundle. Contains markup for the component or app. Each bundle contains only one component or app resource. |
Controller | sampleController.js |
Contains client-side controller methods to handle events in the component. |
Helper | sampleHelper.js |
JavaScript functions that can be called from any JavaScript code in a component’s bundle |
CSS Styles | sample.css |
Contains styles for the component. |
Documentation | sample.auradoc |
A description, sample code, and one or multiple references to example components |
Renderer | sampleRenderer.js |
Client-side renderer to override default rendering for a component. |
Design | sample.design |
File required for components used in Lightning App Builder, Lightning pages, Community Builder, or Cloud Flow Designer. |
SVG File | sample.svg |
Custom icon resource for components used in the Lightning App Builder or Community Builder. |
In this example we will fetch Picklist field “Industry” values from “Account” object and set in ui:inputSelect
on Lightning Component.
Apex Class:
public class GetPicklistValuesController { @AuraEnabled public static List <String> getPicklistValues(sObject obj, String fld) { List <String> pList = new list <String>(); //Get the object type of the SObject. Schema.sObjectType objType = obj.getSObjectType(); //Describe the SObject using its object type. Schema.DescribeSObjectResult objDescribe = objType.getDescribe(); //Get a map of fields for the SObject Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap(); //Get the list of picklist values for this field. List<Schema.PicklistEntry > values = fieldMap.get(fld).getDescribe().getPickListValues(); //Add these values to the selectoption list. for (Schema.PicklistEntry a: values) { pList.add(a.getValue()); } pList.sort(); return pList; } }
Component:
<!--TestComponent--> <aura:component controller="GetPicklistValuesController" access="global" implements="force:appHostable"> <aura:attribute name="objInfo" type="account" default="{sobjectType : 'Account'}" /> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <div class="slds-form-element"> <label class="slds-form-element__label" for="select-01">Select Industry</label> <div class="slds-select_container"> <ui:inputSelect aura:id="accIndustry" class="slds-select" change="{!c.onPicklistChange}"/> </div> </div> </aura:component>
Component Helper:
({ fetchPickListVal: function(component, fieldName, elementId) { var action = component.get("c.getPicklistValues"); action.setParams({ "obj": component.get("v.objInfo"), "fld": fieldName }); var opts = []; action.setCallback(this, function(response) { var state = response.getState(); if (state == "SUCCESS") { var allValues = response.getReturnValue(); opts.push({ class: "optionClass", label: "--- None ---", value: "" }); for (var i = 0; i < allValues.length; i++) { opts.push({ class: "optionClass", label: allValues[i], value: allValues[i] }); } component.find(elementId).set("v.options", opts); } }); $A.enqueueAction(action); }, })
Component Controller:
({ doInit: function(component, event, helper) { helper.fetchPickListVal(component, 'Industry', 'accIndustry'); }, onPicklistChange: function(component, event, helper) { //get the value of select option alert(event.getSource().get("v.value")); }, })
Lightning App:
<!--TestApp--> <aura:application extends="ltng:outApp" access="global"> <c:TestComponent /> </aura:application>