You need to sign in to do that
Don't have an account?
ZG
Unable to update an Apex Class in Lightning helper function
I can't find a good example of a custom Apex class being set with default values on the Lightning component and then its values being updated in the helper function. The code below is not updating the object values retrieved from component.get even thought I have JSON.parse(JSON.stringify) to create a new editable object. What is the correct way to do this?
The output on the console is:
Apex class public class Claim { public ID ContactID; public ID AccountID; public string RetireeNumber; public string recordType; public Claim() { } } <aura:component controller="ClaimController"> <aura:attribute name="claim" type="Claim" default="{ContactID: '7890', AccountID: '12345', RetireeNumber': '6789', recordType: 'Rec Type 1', MedicalCodes: null}" /> helper class createMedicalCode: function(component...) { let claim = component.get("v.claim"); console.log('claim:' + claim); claim.RetireeNumber = 'hello'; claim.recordType = 'Claim'; console.log('updated claim:' + claim); let newClaim = JSON.parse(JSON.stringify(claim)); newClaim.RetireeNumber = 'hello'; newClaim.recordType = 'Claim'; console.log('newClaim' + newClaim); console.log('newClaim.RetireeNumber: '+ newClaim.RetireeNumber); console.log('newClaim["RetireeNumber"]: ' + newClaim["RetireeNumber"]); ... } })
The output on the console is:
claim:{ContactID: '7890', AccountID: '12345', RetireeNumber': '6789', recordType: 'Claim', MedicalCodes: null} updated claim:{ContactID: '7890', AccountID: '12345', RetireeNumber': '6789', recordType: 'Claim', MedicalCodes: null} newClaim{ContactID: '7890', AccountID: '12345', RetireeNumber': '6789', recordType: 'Claim', MedicalCodes: null} newClaim.RetireeNumber: undefined newClaim["RetireeNumber"]: undefined
Try the below one :
createMedicalCode: function(component...)
{
let claim = component.get("v.claim");
console.log('claim:' + claim);
component.set("v.claim.RetireeNumber","hello");
component.set("v.claim.recordType","Claim");
let updatedClaim = component.get("v.claim");
console.log('updated claim:' + updatedClaim);
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj
I have also tried as per your using let function everything is fines to me:
One small thing in your attribute you have the wanted single quotes nearby RetireeNumber
<aura:attribute name="claim" type="Claim" default="{ContactID: '7890', AccountID: '12345', RetireeNumber': '6789', recordType: 'Rec Type 1', MedicalCodes: null}" />
please remove that single quote and try as like below
<aura:attribute name="claim" type="Claim" default="{ContactID: '7890', AccountID: '12345', RetireeNumber: '6789', recordType: 'Rec Type 1', MedicalCodes: null}" />
I found out that just creating a Javascript object and running JSON.stringify on it and passing it to the server method works, so maybe I'll take that route instead of trying to read/update the object using Lighnting methods. I'll create a brand new JS object with the values from the component, update the values on the JS object then serialize and send them to the server.
component.set("v.claim.RetireeNumber","hello");
component.set("v.claim.recordType","Claim");
<aura:component controller="ClaimController">
Could you try it on your end to see if you're able to update the Claim object in JS?