0

Component

<aura:component controller="WrapperClassLightning">
    <aura:attribute name="accountRecord" type="Account" 
                    default="{sobjectType:'Account', 'name':'Test Account','Active__c':'yes'}"/>
    <aura:attribute name="accId" type="String"/>
    <aura:attribute name="accountList" type="List"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:handler name="change" value="{!accId}" action="{!c.getRecord}"/>
    <lightning:select name="accountPickList"  label="Select Account" value="{!v.accId}">
        <aura:iteration items="{!v.accountList}" var="acc">
            <option text="" value=""/>
            <option text="{!acc.Name}" value="{!acc.Id}"></option>
        </aura:iteration>
    </lightning:select>
     <ui:inputText label="AccountName" value="{!v.accountRecord.Name}" updateOn="click"/>
</aura:component>

JS Controller

({
    doInit : function(component, event, helper) {
        var action=component.get('c.init');// if we have to call any apex class method then we use this statement
        action.setCallback(this,function(response){
            var state= response.getState();

            if(state == 'SUCCESS'){

                component.set('v.accountList', response.getReturnValue());
            }

        });
        $A.enqueueAction(action);
    },
    getRecord : function(component, event, helper){
        alert('here');
        var accId = component.get('v.selectAccount');
        var action = component.get('c.getRecord');// if we have to call any apex class method then we use this statement
        action.getParams({'recordId':accId});
        action.setCallback(this,function(response){
            var state= response.getState();

            if(state == 'SUCCESS'){

                component.set('v.accountRecord', response.getReturnValue());
                alert(v.accountRecord);
            }

        });
        $A.enqueueAction(action);
    }
})

closed as off-topic by glls, Jayant Das, Pranay Jaiswal, Mark Pond, David Reed Dec 14 '18 at 2:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – glls, Jayant Das, Pranay Jaiswal
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    You only have code here, what is your issue? Take a moment to go through How to Ask, and then edit your question to provide more details with specific issue that you are facing. – Jayant Das Dec 13 '18 at 17:59
  • 1
    Have you taken a look at trailhead? You will get many examples there as how you can achieve db operations with lightning components. – Jayant Das Dec 13 '18 at 18:46
  • 1
    Trailhead has a few great modules about working with data using the Lightning Data Service or using Apex. Both are worth reading. – Mark Pond Dec 13 '18 at 18:47
1

I certainly agree with the commentators on your question that this question is both a bit unclear, and should probably be researched a bit further before coming to the stack exchange community for help. That said, since I had a similar struggle at the start of my apex development, I will take a shot at it nonetheless.

The piece that you seem to be missing here is that in a lightning component that updates or needs to query your salesforce data, you actually need TWO controllers. One of them you have already, the JS controller. The other piece that you need to include is an Apex server controller. The way to do this is to create an apex class as you would any other in the dev console. Here you can create the method that actually does the database updates.

The tricky part is tying this into your lightning component so that your JS controller knows where to pass the parameters. There are a few things you need to do to make this happen.

It appears you have already done the first part of this, which is to mark a controller in your aura component file. At the top element of your component, you have a line that includes

controller="WrapperClassLightning"

What this should do is indicate to your JS controller that any call to any apex will go to a class with the name 'WrapperClassLightning'. If this is not the name of your apex controller class, this may be your first issue.

Lets look at your JS Controller next. You have a line early that says-

var action=component.get('c.init');

What this is saying is that control should be passed to the Apex controller class 'WrapperClassLightning' and execute the method 'init'. If you do not have a method named init in your class, this may be another issue.

If you are just doing an update, this should really be all you need as far as you need, as you won't need to worry about a callback unless you are passing the updated data or a query back from the apex class to the lightning component. It appears you have some logic to do something like that, but it generally looks like correct usage so I'm not going to dig into that too much.

In any case, as long as you have marked the correct controller class in your aura component, and are calling the right method name in your JS controller, you should be able to access the apex where you can then make whatever updates you need, and update them in the system with a DML method.

I STRONGLY recommend that you read up on this topic further, as it will come up quite often in lightning development, and follows the same basic design pattern for any component that needs to reach into the data and reference any record data.

Not the answer you're looking for? Browse other questions tagged or ask your own question.