I am experimenting with the example of Trailhead AccountList lightning components. The part to get server-side to client-side works.
I am trying to pass a value from client-side to server-side but this doesn't give me a value.
AccountlistController.js
({
doInit: function(component, event, helper) {
// Fetch the account list from the Apex controller
helper.getAccountList(component);
},
IdAccount: function(component, event, helper) {
// Prevent the form from getting submitted
event.preventDefault();
// Get the value from the field that's in the form
var accountName = event.target.getElementsByClassName('account-name')[0].value;
var accountId = event.target.getElementsByClassName('account-id')[0].value;
confirm('Set the id '+ accountId +'For '+ accountName + ' account? ');
var action = component.get("c.ServerAccountEcho");
action.setParams({ recordId : accountId });// setting the parameter to apex class method
}
})
AccountsController
public class AccountsController {
@AuraEnabled
public static List<Account> getAccounts() {
return [SELECT id,Name FROM Account];
}
public static void ServerAccountEcho(id recordId) {
system.debug('recordId ' + recordId);
List<User> U = [Select id,Name, CurrentAccountIdComm__c FROM User WHERE Firstname='Alex'];
U[0].CurrentAccountIdComm__c = 'echo';
update U;
}
}
Component
<aura:component controller="AccountsController" implements="flexipage:availableForRecordHome" access="global">
<aura:attribute name="accounts" type="List" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<!--
Use a data table from the Lightning Design System:
https://www.lightningdesignsystem.com/components/data-tables/
-->
<table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout">
<thead>
<tr class="slds-text-heading--label">
<th scope="col"><div class="slds-truncate" title="ID">ID</div></th>
<th scope="col"><div class="slds-truncate" title="Name">Name</div></th>
</tr>
</thead>
<tbody>
<!-- Use the Apex model and controller to fetch server side data -->
<aura:iteration items="{!v.accounts}" var="account">
<tr>
<th scope="row"><div class="slds-truncate" title="{!account.Id}">{!account.Id}</div></th>
<td><div class="slds-truncate" title="{!account.Name}">{!account.Name}</div></td>
<td>
<form class="account-form" onsubmit="{!c.IdAccount}">
<input type="hidden" value="{!account.Name}" class="account-name" />
<input type="hidden" value="{!account.Id}" class="account-id" />
<lightning:button iconName="utility:forward"
iconPosition="left"
variant="neutral"
type="submit"
/>
</form>
</td>
</tr>
</aura:iteration>
</tbody>
</table>
</aura:component>
ServerAccountEcho()
method since you are passing in a recordId to the server and expecting a user record to be updated. Can you post the Component's markup or the code that is calling yourIdAccount()
function?