function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kamal ThakurKamal Thakur 

Update Record using Lightning Button

I am opening a Lightning App on a button Click (via URL). The App updates Description field on Contact Object. Below is my code -
ConApp
<aura:application >
    <aura:attribute name="id" type="String"/>
	hi {!v.id}
    <c:ConAppCmp conid="{!v.id}" />
</aura:application>

conAppCmp
<aura:component controller="MyControl">
    <aura:attribute name="conObj" type="Contact" />
    <aura:attribute name="conid" type="String"/>
    <p>hi {!v.conid}</p>
    <aura:attribute name="replaceProducts" type="List" default="['red','blue']"/>
    <ui:inputSelect aura:id="levels" label="Available Replacements">
        <aura:iteration items="{!v.replaceProducts}" var="level">
             <ui:inputSelectOption text="{!level}"  value="{!v.conObj.Description}" label="{!level}"/>
        </aura:iteration>
    </ui:inputSelect>
    <lightning:button variant="brand" label="Submit" onclick="{! c.handleClick }" />
</aura:component>

conAppCmpController.js
({
	handleClick : function(component, event, helper) {
		var newAcc = component.get("v.conObj");
        var action = component.get("c.saveRecord");
        action.setParams({ 
        "acc": newAcc
            });
        action.setCallback(this, function(a) {
           var state = a.getState();
            if (state === "SUCCESS") {
                var name = a.getReturnValue();
               alert("hello from here"+name);
            }
        });
         $A.enqueueAction(action)

	}
})

myControl
public with sharing class MyControl {
@AuraEnabled
    public static Contact saveRecord(Contact acc) {
        upsert acc;
        return acc;
    }
}
Button Contains this URL - 
/c/ConApp.app?id={!Contact.Id}

I don't know what is causing the problem.
 
Best Answer chosen by Kamal Thakur
Aman MalikAman Malik
Hi Kamal,
It seems, you are not able to get selected value on button click. Please correct me if i am wrong.
To get selected value, create an attribute which will bind to your picklist and then get in controller using that attribute like below

conAppCmp
<aura:component controller="MyControl">
    <aura:attribute name="conObj" type="Contact" />
    <aura:attribute name="conid" type="String"/>
    <aura:attribute name="selectedProduct" type="String"/>
    <p>hi {!v.conid}</p>
    <aura:attribute name="replaceProducts" type="List" default="['red','blue']"/>
    <ui:inputSelect aura:id="levels" label="Available Replacements" value="{!v.selectedProduct}">
        <aura:iteration items="{!v.replaceProducts}" var="level">
             <ui:inputSelectOption text="{!level}" label="{!level}"/>
        </aura:iteration>
    </ui:inputSelect>
    <lightning:button variant="brand" label="Submit" onclick="{! c.handleClick }" />
</aura:component>

conAppCmpController.js
({
    handleClick : function(component, event, helper) {
        var selectedProduct = component.get("v.selectedProduct");
        console.log('---'+selectedProduct);
    }
})
Kindly let me know if this works for you.

 

All Answers

Aman MalikAman Malik
Hi Kamal,
It seems, you are not able to get selected value on button click. Please correct me if i am wrong.
To get selected value, create an attribute which will bind to your picklist and then get in controller using that attribute like below

conAppCmp
<aura:component controller="MyControl">
    <aura:attribute name="conObj" type="Contact" />
    <aura:attribute name="conid" type="String"/>
    <aura:attribute name="selectedProduct" type="String"/>
    <p>hi {!v.conid}</p>
    <aura:attribute name="replaceProducts" type="List" default="['red','blue']"/>
    <ui:inputSelect aura:id="levels" label="Available Replacements" value="{!v.selectedProduct}">
        <aura:iteration items="{!v.replaceProducts}" var="level">
             <ui:inputSelectOption text="{!level}" label="{!level}"/>
        </aura:iteration>
    </ui:inputSelect>
    <lightning:button variant="brand" label="Submit" onclick="{! c.handleClick }" />
</aura:component>

conAppCmpController.js
({
    handleClick : function(component, event, helper) {
        var selectedProduct = component.get("v.selectedProduct");
        console.log('---'+selectedProduct);
    }
})
Kindly let me know if this works for you.

 
This was selected as the best answer
Kamal ThakurKamal Thakur
Thank you @Aman, it worked like a charm.