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
Dev87Dev87 

lightning Component - Save records


Hello, That's my problem.
I have a list of buttons in my lightning page. The button labels are retrieved from a table SF (iterations). The idea of ​​registering opportunities with the name of the label buttons.

my label-buttion itération:
                <div class="slds-col slds-size--1-of-12" >
                 <aura:iteration items="{!v.zones}" var="item">                
                <lightning:button label="{!item}" aura:id ="{!item}" name ="{!item}" class="{!'customButton' + v.indexBtn + ' slds-size--1-of-1 slds-p-horizontal_x-small'}" onclick="{!c.myAction}"/>
                   </aura:iteration>
                </div>  


can someone help me.
 
Nayana KNayana K
public class OpportunityCreation
{

	@AuraEnabled
	public Id createOpp(String oppName)
	{
		Opportunity objOpp = new Opportunity(Name = oppName, CloseDate = Date.today().addDays(5), Stage = 'Needs Analysis');
		insert objOpp;
		return objOpp.Id;
	}
}

Set controller in the component like 
<aura:component controller="OpportunityCreation">
// all your code
</aura:component>
 
({
    "echo" : function(cmp, event, helper) {
	
        // create a one-time use instance of the OpportunityCreation action
        // in the server-side controller
        var action = cmp.get("c.createOpp");
        action.setParams({ "oppName" : event.getSource().get("v.label") });

        // Create a callback that is executed after 
        // the server-side action returns
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                // Alert the user with the value returned 
                // from the server
				// here opportunity id will be returned
                alert("From server: " + response.getReturnValue());

                // You would typically fire a event here to trigger 
                // client-side notification that the server-side 
                // action is complete
            }
            else if (state === "INCOMPLETE") {
                // do something
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });

        // optionally set storable, abortable, background flag here

        // A client-side action could cause multiple events, 
        // which could trigger other events and 
        // other server-side action calls.
        // $A.enqueueAction adds the server-side action to the queue.
        $A.enqueueAction(action);
    }
})

 
Dev87Dev87
Thank u for u're response, but in my case i would not click on  this button.  
Dev87Dev87
I tried to get a custom class for each button using "v.indexBtn", but that simply not working.
Amir BOUTITIAmir BOUTITI
Use the "indexVar" attribute to get the index of each item inside the iteration.
<div class="slds-col slds-size--1-of-12" >
	<aura:iteration items="{!v.zones}" var="item" indexVar="index">                
        <lightning:button label="{!item}" aura:id ="{!item}" name ="{!item}" class="{!'customButton' + index + ' slds-size--1-of-1 slds-p-horizontal_x-small'}" onclick="{!c.myAction}"/>
	</aura:iteration>
</div>