You need to sign in to do that
Don't have an account?
Avinash Gadgil 3
I have following code to fetch the List of users in LWC using APex I am unable to fetch profile name from User. It is showing in console log but not showing in Lightning data table. How to show the same in Lightning data table in LWC
Apex Code:
public with sharing class userListApexClassForLWC {
@AuraEnabled(cacheable=true)
public static List<User> userList() {
return [Select Id, FirstName, LastName, Email, profile.Name, IsActive FROM User Where IsActive=true LIMIT 5];
}
}
LWC JS:
import { LightningElement, wire, track} from 'lwc';
import userListApexClassForLWC1 from '@salesforce/apex/userListApexClassForLWC.userList';
export default class UserListInLWC extends LightningElement {
@track error;
@track FirstName;
@track LastName;
@track Email;
@track profile;
@track IsActive;
profileName;
data=[]
columns = []
@track columns = [
{ label: 'First Name', fieldName: 'FirstName', type: 'text' },
{ label: 'Last Name', fieldName: 'LastName', type: 'text' },
{ label: 'Email', fieldName: 'Email', type: 'email' },
{ label: 'Profile', fieldName: 'profileName', type: 'string' },
{ label: 'IsActive', fieldName: 'IsActive', type: 'boolean' },
];
@wire(userListWrapper, {currentRecordId: '$recordId'})
dataFn({data}){
this.data = data;
}
@wire (userListApexClassForLWC1) wireduser ({error,data})
{
if (error) {
this.error = error ;
} else if (data) {
this.data = data;
profileName= data.profile.name;
console.log(data);
}
}
}
LWC HTML:
<template>
<lightning:card>
<div style="height: 500px;">
<template if:true={data}>
<lightning-datatable
key-field="id"
data={data}
columns={columns}>
</lightning-datatable>
</template>
</div>
</lightning:card>
</template>
public with sharing class userListApexClassForLWC {
@AuraEnabled(cacheable=true)
public static List<User> userList() {
return [Select Id, FirstName, LastName, Email, profile.Name, IsActive FROM User Where IsActive=true LIMIT 5];
}
}
LWC JS:
import { LightningElement, wire, track} from 'lwc';
import userListApexClassForLWC1 from '@salesforce/apex/userListApexClassForLWC.userList';
export default class UserListInLWC extends LightningElement {
@track error;
@track FirstName;
@track LastName;
@track Email;
@track profile;
@track IsActive;
profileName;
data=[]
columns = []
@track columns = [
{ label: 'First Name', fieldName: 'FirstName', type: 'text' },
{ label: 'Last Name', fieldName: 'LastName', type: 'text' },
{ label: 'Email', fieldName: 'Email', type: 'email' },
{ label: 'Profile', fieldName: 'profileName', type: 'string' },
{ label: 'IsActive', fieldName: 'IsActive', type: 'boolean' },
];
@wire(userListWrapper, {currentRecordId: '$recordId'})
dataFn({data}){
this.data = data;
}
@wire (userListApexClassForLWC1) wireduser ({error,data})
{
if (error) {
this.error = error ;
} else if (data) {
this.data = data;
profileName= data.profile.name;
console.log(data);
}
}
}
LWC HTML:
<template>
<lightning:card>
<div style="height: 500px;">
<template if:true={data}>
<lightning-datatable
key-field="id"
data={data}
columns={columns}>
</lightning-datatable>
</template>
</div>
</lightning:card>
</template>
Does this similar post https://salesforce.stackexchange.com/questions/350198/lwc-data-shows-in-console-but-foreach-fails-to-display-it help?
Thanks
Try updating your wire method like so -
@wire (userListApexClassForLWC1) wireduser ({error,data})
{
if (error) {
this.error = error ;
} else if (data) {
const dataList = [];
data.forEach(record => {
const dataRow = {};
dataRow.FirstName = record.FirstName;
dataRow.LastName = record.LastName;
dataRow.Email = record.Email;
dataRow.IsActive = record.IsActive;
dataRow.profileName = record.profile.Name;
dataList.push(dataRow);
});
this.data = dataList;
console.log(data);
}
}