Lightning Picklist/Select : Aura vs LWC
Lightning Picklist/Select : Aura Component
PickListOptionsController(Apex Controller )
public class PickListOptionsController {
@AuraEnabled
public static List getPickListOptionList(String objectName, String picklistFieldName){
List picklistOptionList = new List();
Schema.SObjectType convertToObj = Schema.getGlobalDescribe().get(objectName);
Schema.DescribeSObjectResult res = convertToObj.getDescribe();
Schema.DescribeFieldResult fieldResult = res.fields.getMap().get(picklistFieldName).getDescribe();
List ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry pickListVal : ple){
picklistOptionList.add(pickListVal.getLabel());
}
return picklistOptionList;
}
}
PickListOptionsComponent.cmp (Component)
PickListOptionsComponent.js (JS Controller )
({
doInit : function(component) {
var action = component.get("c.getPickListOptionList");
action.setParams({
objectName: component.get("v.objectName"),
picklistFieldName: component.get("v.picklistFieldName")
});
action.setCallback(this, function(response) {
var list = response.getReturnValue();
component.set("v.picklistValues", list);
})
$A.enqueueAction(action);
}
})
Lightning Picklist/Select : Lightning Web Component
LightningPicklist.html (Template)
LightningPicklist.Js (JS Controller )
import { LightningElement,wire,api,track } from 'lwc';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import OPPORTUNTITY_OBJECT from '@salesforce/schema/Opportunity';
import STAGE_NAME_FIELD from '@salesforce/schema/Opportunity.StageName';
export default class LocalMattersGroupDetails extends LightningElement {
@track stageNamePicklist='';
strTitle=''
stageName='OPPORTUNTITY STAGE';
//Get Object Information
@wire(getObjectInfo, { objectApiName: OPPORTUNTITY_OBJECT})
objectInfo;
// 'Group' is recordType name on OPPORTUNTITY_OBJECT
//Get RecordTypeId
get recordTypeId() {
const rtis = this.objectInfo.data.recordTypeInfos;
return Object.keys(rtis).find(rti => rtis[rti].name === 'Group');
}
//Get PickList Information : OPPORTUNTITY_STAGE
@wire(getPicklistValues, { recordTypeId: '$objectInfo.data.defaultRecordTypeId', fieldApiName: STAGE_NAME_FIELD})
stageNamePicklist;
handleStageNameChange(event) {
this.stageName= event.target.value;
// eslint-disable-next-line no-console
console.log("Stage Name :", this.stageName);
}
}
LightningPicklist.js-meta.xml (XML for Config File in Salesforce)
45.0
true
lightning__RecordPage
Note: After save whole code you will drag and drop code into Lightning Record Detail Page of Opportunity
Comments
Post a Comment