Generic component which has following features: (This can be used in both AURA and LWC parent component)
You can find the component here: https://github.com/sasank-sfdcdev/public/tree/master/src/lwc/datatable
- Gets data from database automatically. Can use relationship fields also.
- Sort functionality (across pages)
- Pagination - first, previous, next and last pages
- Persistant selection of records across pages. getSelectedRows public method to get selected data.
- All events of lightning-datatable plus event while loading data
- Cacheable data
- Sosl search
- Dynamically change data filters
The simplicity of this is such that, you can just put tag, and rest of the things is done by it. Example HTML:
<template>
<c-datatable config={config}>
</c-datatable>
</template>
and its JS:
import { LightningElement, track } from 'lwc';
export default class PocTable extends LightningElement {
config = {
objectName: "Account",
tableConfig: {
columns: [
{ api: 'Name', label: 'Name', fieldName: 'Name', sortable: true },
{ api: 'CreatedDate', label: 'Created On', fieldName: 'CreatedDate', type: 'date', sortable: true },
{ api: 'CreatedBy.Name', label: 'Created By', fieldName: 'CreatedByName', sortable: true }
]
}
};
}
NOTE: You need add api
property in columns
which will be used to fetch data from database.
Here is how it will render:
Here is the sample of AURA component:
<aura:component implements="lightning:isUrlAddressable,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access = "GLOBAL">
<aura:attribute name="config" type="Map" default="{}" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<c:datatable config="{!v.config}" />
</aura:component>
and controller.js
,
({
doInit : function(component, event, helper) {
component.set("v.config", {
objectName: "Account",
tableConfig: {
columns: [
{ api: 'Name', label: 'Name', fieldName: 'Name', sortable: true },
{ api: 'CreatedDate', label: 'Created On', fieldName: 'CreatedDate', type: 'date', sortable: true },
{ api: 'CreatedBy.Name', label: 'Created By', fieldName: 'CreatedByName', sortable: true }
]
}
});
},
})
I am wondering if this is a timing issue, where you are calling testMe before the getRecord is finished, though I'd need to see your template to be sure.
But I re-created your code in a scratch org, and it worked ok when I used this as my template, but without the if:true, both Name and Service Area are undefined
<template>
<template if:true={app}>
Test Me: {testMe}
</template>
</template>
And my controller:
import { LightningElement, api, track, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import APP_NAME from '@salesforce/schema/Application__c.Name';
import APP_SERVICEAREA from '@salesforce/schema/Application__c.Service_Area__c';
export default class StackAppRecord extends LightningElement {
@api recordId; // Application__c.Id
@track app; // Application__c record
@track appName;
// ...
@wire(getRecord, { recordId: '$recordId', fields: [APP_NAME,APP_SERVICEAREA] })
loadRecord ({ error, data }) {
if (data) {
this.app = data;
this.error = undefined;
console.log('got data');
} else if (error) {
// ...
}
}
// Called from a {testMe} in the HTML template
get testMe() {
console.log(this.app); // returns a Proxy
console.log(JSON.stringify(this.app)); // returns a JSON as expected:
console.log(this.app.fields.Name.value); // shows Name Value:
console.log(this.app.fields.Service_Area__c.value); // show Service Value:
return '?!';
}
}
Best Answer
My take on this would be,
lightning/uiRecordApi
is very very primitive, it's as good as using Standard Rest/Soap API to do CRUD from SF or from 3rd party.Following factors you need to consider before you go this route:
lightning/uiRecordApi
does not support bulk records updates, this means you have to call it multiple times(In case you wanna update multiple rows of the data table or multiple objects)All and all, if it's very primitive crud work you wanna do, go for
uiRecordApi
, if its more complex stick with apex.Prior to LWC or Lightning, we used to use JSForce to do simple crud, my opinion is uiRecordApi is SF native solution to this.