You need to sign in to do that
Don't have an account?
Soundar Rajan Ponpandi
How Can I Display Picklist values From Record ?
HI,
I would like to display a picklist value from record.
1. I am using one custom object it's called "GD_Offer__c ". I want to display a field as a picklist with reference of "GD_Offer__c" records.
2. For example i have 3 following records in this "GD_Offer__c" table, I need to display those records in picklist value.
A. Offer A
B. Offer B
C. Offer C
I wrote coding for this functionality, I am not sure where i did a mistake. Can you please guide me where i did a mistake and what are all the changes i need to do to achieve mentioned functionality.
Apex Class Method
Component
Controller.JS
Helper.JS
Thanks In Advance,
Soundar.
Thanskn
I would like to display a picklist value from record.
1. I am using one custom object it's called "GD_Offer__c ". I want to display a field as a picklist with reference of "GD_Offer__c" records.
2. For example i have 3 following records in this "GD_Offer__c" table, I need to display those records in picklist value.
A. Offer A
B. Offer B
C. Offer C
I wrote coding for this functionality, I am not sure where i did a mistake. Can you please guide me where i did a mistake and what are all the changes i need to do to achieve mentioned functionality.
Apex Class Method
@AuraEnabled public static Map<String, String> getOffers(){ system.debug('Entered into the Get Offers ********'); Map<String, String> options = new Map<String, String>(); Schema.DescribeFieldResult fieldResult = GD_Offers__c.Name.getDescribe(); system.debug('fieldResult | ' + fieldResult); List<Schema.PicklistEntry> pList = fieldResult.getPicklistValues(); system.debug('pList ' + pList); for (Schema.PicklistEntry p : pList) { options.put(p.getValue(), p.getLabel()); } system.debug('Options |' + options); return options; }
Component
<aura:attribute name="offerMap" type="Map"/> <table> <tr> <td> <lightning:recordEditForm aura:id="form" objectApiName="GD_Quote_Line_Items__c" > <div class="form-group"> <lightning:select aura:id="industryPicklist" value="{!v.lineItem.GD_Offers__c}" onchange="{!c.handleCompanyOnChange}"> <option value="">--None--</option> <aura:iteration items="{!v.offerMap}" var="ind" indexVar="key"> <option text="{!ind.value}" value="{!ind.key}" selected="{!ind.key==v.lineItem.GD_Offers__c}" /> </aura:iteration> </lightning:select> </div> </lightning:recordEditForm> </td> </tr> </table>
Controller.JS
doInit: function(component, event, helper) { helper.getOfferPicklist(component, event); },
Helper.JS
getOfferPicklist: function(component, event) { alert('Entered Offer Picklist'); var action = component.get("c.getOffers"); action.setCallback(this, function(response) { var state = response.getState(); alert('State = ' + state); if (state === "SUCCESS") { var result = response.getReturnValue(); var offerMap = []; for(var key in result){ offerMap.push({key: key, value: result[key]}); } component.set("v.offerMap", offerMap); } }); $A.enqueueAction(action); },
Thanks In Advance,
Soundar.
Thanskn
I did few changes in my coding. Now it's working as expected.
Core Changes
Controller.JS
Apex class
I am choosing my answer as best answer for othres reference.
Regards,
Soundar.
All Answers
1. Apex method getOffers.
You used schema.DescribeFieldResult. The class is to get the picklist values from a field.
In your case, you have three records in GD_Offer__c object.
Simply use soql to get the records and build the map.
list<gd_offer__c> gdoffers = [select name from gd_offer__c];
2. In Aura component html.
You use <lightning:recordEditForm> to create the record. <lightning:recordEditForm> is commonly used to use Salesforce data service without create your own submit handler (apex method).
<lightning:recordEditForm> can talk to field directly. It is usually having <lighting:inputfield> wrapped.
i.e.
<lightning:recordEditForm recordId="003XXXXXXXXXXXXXXX" objectApiName="Contact">
<lightning:messages />
<lightning:outputField fieldName="AccountId" />
<lightning:inputField fieldName="FirstName" />
<lightning:inputField fieldName="LastName" />
<lightning:inputField fieldName="Email" />
</lightning:recordEditForm>
So, you might not use Lighntining data service. Instead, put the filed on the lighting component and create your own apex method to save the data.
You can use lightning:combobox component to display the picklist,consequently, modify this line: <aura:attribute name="offerMap" type="Map"/>
to <aura:attribute name="offerMap" type="List"/> and modifiy js helper controller when populating the data.
See sample here
https://developer.salesforce.com/docs/component-library/bundle/lightning:combobox/example
Thanks for your quik response.
I have changed my coding as well, Now this offer value could not pass into the save method. Can you please check and let me know where i did mistake.
Component
Controller.JS
Helper.JS
Apex Controller
Thanks in Advance,
Soundar.
That code is not working properly.
Exactly i am looking for,
1. Display Offer Lookup as a picklist (I did by using List method).
2. Now Selected picklist Value shoud update in the lookup field (I am struggling on that)
Regards,
Soundar.
I did few changes in my coding. Now it's working as expected.
Core Changes
Controller.JS
Apex class
I am choosing my answer as best answer for othres reference.
Regards,
Soundar.