function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ZGZG 

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?
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

 
Maharajan CMaharajan C
Hi,

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
Maharajan CMaharajan C
Hi,

I have also tried as per your using let function everything is fines to me:

User-added image

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}"     />
ZGZG
Hi Maharajan, it didn't work for me. I'll try again.

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.
ZGZG
component.get("v.claim.RetireeNumber") returns undefined after the lines below:

component.set("v.claim.RetireeNumber","hello");
component.set("v.claim.recordType","Claim");
ZGZG
Maharajan, what does your <aura:component look like? That's what I have. Maybe I have to add an "implements" attribute?

<aura:component controller="ClaimController">
Raj VakatiRaj Vakati
YOU apex class will be like this .. you need to  to use @AuraEnabled
public class Claim {

        @AuraEnabled
    public ID ContactID;

        @AuraEnabled
    public ID AccountID;

        @AuraEnabled
    public string RetireeNumber;

        @AuraEnabled
    public string recordType; 
  
    public Claim()
    {
        
    }
}

 
ZGZG
Hi Raj, I changed the Claim class to the code below, still no luck.
 
public class Claim {

    @AuraEnabled
    public ID ContactID {get; set;}
    @AuraEnabled
    public ID AccountID {get; set;}
    @AuraEnabled
    public string RetireeNumber {get; set;}
    @AuraEnabled
    public string recordType {get; set;} // 'Claim' or 'Preauthorization'
    @AuraEnabled
    public List<MedicalCode> MedicalCodes {get; set;}
    
    public Claim()
    {
        
    }
}

 
Raj VakatiRaj Vakati
What about the MedicalCode wrapper class ??  all variable need to be  @AuraEnabled 
ZGZG
Still not working with. This is how the classes look now:
 
public class Claim {

    @AuraEnabled
    public ID ContactID {get; set;}
    @AuraEnabled
    public ID AccountID {get; set;}
    @AuraEnabled
    public string RetireeNumber {get; set;}
    @AuraEnabled
    public string recordType {get; set;} // 'Claim' or 'Preauthorization'
    @AuraEnabled
    public List<MedicalCode> MedicalCodes {get; set;}
    
    public Claim()
    {
        
    }
}


public class MedicalCode {
    @AuraEnabled public string Code {get;set;}
    @AuraEnabled public boolean isPrimary {get;set;}
    
    public MedicalCode()
    {
        
    }
}



Could you try it on your end to see if you're able to update the Claim object in JS?