An issue with the Apex controller / LWC interaction is that when retrieving values from an Apex controller / method the returned object will not bring back object properties that do not have values.
for example if we are trying to retrieve an Account from the database, we use @wire, call the apex method and within the Apex method utilize a query to retrieve necessary values.
return SELECT Id, Name, Occupation__c from Account LIMIT 1
the issue is that if Occupation__c is null in the database, then the field name "Occupation__c" is not even returned in the object passed back to the LWC. Which brings me to my question, how best to handle this in LWC?
currently I am using this method to "validate" the values coming back from the Apex method
this.account = {
AccountId: data.Id,
OwnerId: data.OwnerId ? data.OwnerId : "",
Occupation: data.Occupation__c ? data.Occupation__c : "",
}
My Question is, is this a more efficient way to do this, is this best practice? does Salesforce have any examples to reference?
Best Answer
Well,
You can get more information here: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_wire_adapters_record
The best approach if you still want to use the apex controler is save the data in an object and then resolve those using getters:
More info here: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/apex