+ Start a Discussion
niki s 7niki s 7 

Pass data from js to apex

Hello I am creating an ui for account. It contains 2 fields name and city. There will be a button which will display list of account. Now while passing values from js to apex getting name as null but in alert it's showing the value 
Suraj Tripathi 47Suraj Tripathi 47
Hi niki s 7,
Here is the solution:

Aura Component:
<aura:component controller="Accountsfields">
   
    <aura:attribute name="Name" type="String" />
    <aura:attribute name="City" type="String"/>
    <div class="slds-p-around_small">
        <header class="slds-modal__header">
            <h2 id="modal-heading-01" class="slds-text-heading_medium slds-hyphenate"> <lightning:icon iconName="standard:account" alternativeText="Account" title="Account" /><b> Account</b></h2>
        </header>
        
        <lightning:input type="Text" aura:id="namefield" label="Name" value="{!v.Name}"/>
        <lightning:input type="text" aura:id="cityfield" label="City" value="{!v.City}"/>
        <footer class="slds-modal__footer slds-align_absolute-center">
                        <lightning:button label="Save" iconPosition="left"   variant="brand" onclick="{!c.doSave}"/>
        </footer>   
    </div>
</aura:component>

Js:
({
    doSave: function(c,e,h){
        try{
            
        var name  = c.get("v.Name");
             console.log('result'+name);
        var city  = c.get("v.City");
             console.log('result'+city);
        
       let action = c.get("c.getaccountfields");
         action.setParams({
            "Name" : name,
             "city" : city
        });
        action.setCallback(this, function(response){
            let state = response.getState();
            if(state === "SUCCESS"){
                console.log(JSON.stringify(response.getReturnValue()));
                JSON.stringify(response.getReturnValue());
                var result = response.getReturnValue();
                console.log('result'+result);
            }
        });
        $A.enqueueAction(action);
        }catch(err){
            console.log(err);
        }
    },
})

Apex Class:

public class Accountsfields {
    @AuraEnabled
     public static string getaccountfields(string Name, string city){
        try{
         system.debug('Name'+ Name);
         system.debug('City'+ city);
            return Name ;
        }catch(exception e){
           return null; 
        }
    }
}


Thanks