You need to sign in to do that
Don't have an account?
Alexander Atkinson
Lightning Component: Create array of accounts in JavaScript with new fields based on component values. Then send to apex for database update
Hi, I'm having understanding how to update my database of accounts with new field values based on component item variables.
Below are the fields, and the values they need to be set to.
JavaScript Controller:
Below are the fields, and the values they need to be set to.
- name = item.title
- id = item.id
- table_number__c = item.status
JavaScript Controller:
updateAccounts: function(component, event, helper) { var action = component.get("c.updateAccounts"); var itemsToPass = component.get("v.allItems"); //Items in the component var AccountsToUpdate = []; //Items that will be sent to //Go through items in the component. //Create a list of accounts. Account name = itemname, Account id = item id, Table Name = Status for (var i=0; i< itemsToPass.length; i++) { var item = itemsToPass[i]; var Account = {Name: item.title, Id: item.Id, Table_Name__c: item.status}; //Push each account to the array. AccountsToUpdate.push(Account); } //Pass the array into Apex list. action.setParams({ "changes": AccountsToUpdate }); $A.enqueueAction(action); }Apex Controller:
public with sharing class AccountController { @AuraEnabled public static List<Account> getAccounts() { List<Account> Accounts = [SELECT Id, Name, TableName__c FROM Account]; return Accounts; } @AuraEnabled public static List<Account> updateAccounts (List<Account> changes) { update changes; return changes; } }Component:
<aura:component> <aura:attribute name="allItems" type="list"></aura:attribute> <lightning:button label="Save Plan" iconName="utility:save" onclick="{!c.updateAccounts}"/> </aura:component>
Greetings to you. It is a pleasure to be in touch with you again.
Please try below code:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.
Thanks and Regards,
Khan Anas
All Answers
Greetings to you. It is a pleasure to be in touch with you again.
Please try below code:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.
Thanks and Regards,
Khan Anas
The console log outputs ItemsToPass and AccountsToUpdate twice and then "ItemsToPass is undefined" at the end.
Are you using a doInit method in JavaScript controller which you was using previously? Please share complete code along with component.
Regards,
Khan Anas
You are using the same JavaScript method name as the Apex controller method (I forgot to change that). According to this post (https://salesforce.stackexchange.com/questions/129261/enqueued-action-not-executed-in-lightning-component), "A javascript method name in a component controller can never be the same name in an apex lightning controller".
Just need to change the name of your javascript method to be different from the apex method.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.
Thanks and Regards,
Khan Anas
I now have one more error. "Error message: Unable to read SObject".
Do you know the cause of this? Thanks alot for all your help!
Please refer to the below link with a similar discussion which might help you further with the above issue.
https://salesforce.stackexchange.com/questions/112636/lightning-components-in-visualforce-unable-to-read-sobject
https://salesforce.stackexchange.com/questions/172720/lightning-component-unable-to-read-sobject
https://salesforce.stackexchange.com/questions/129971/unable-to-read-sobject-error-when-saving-record-from-a-lightning-components-con
https://success.salesforce.com/issues_view?id=a1p3A0000001CNuQAM&title=unable-to-save-data-which-uses-lookup-fields-and-the-fields-are-displayed-using-force-inputfield-in-lightning-component
Regards,
Khan Anas
I can't seem to figure out how to correctly deserialize my array.
Apex Class
Error: Method does not exist or incorrect signature: void deserialize(List<Account>, System.Type) from the type System.JSON
Thanks for the help!