0

With this code I don't get any value , I get the column with the name but their are no value , I don't now what is the mistake .

Some help please .

public with sharing class Action {
    @AuraEnabled(cacheable=true)
    public static List<Action__c> GetAction(String id) {

        Obj__c obj= [Select Id from Obj__c where id =:id];
        List<Plan__c> listAction = new List<Plan__c([SELECTid,Date__c,Theme__c,Name FROM Plan__c where Obj__c =:obj.Id]);

         return listAction;
    }
}

==> JS:

import { LightningElement,track,wire, api} from 'lwc';
import DATE from '@salesforce/label/c.DATE';
import THEME from '@salesforce/label/c.THEME';
import NAME from '@salesforce/label/c.NAME';
import GetAction from'@salesforce/apex/ClassAction.GetAction';

const columns = [
    { label: DATE, fieldName: 'date'}, 
    { label: THEME, fieldName: 'theme' },
    { label: ACTIONNAME, fieldName: 'name' }
];


export default class ActionLWC extends LightningElement {
   
    @api recordId;
    data = [];
    columns = columns;

    @wire(GetAction,{id:'$recordId'})
    wiredGetAction({ error, data }) {
        if (data) {
            let fixeddata = [];
            data.forEach((row) =>{
                let dataline = Object.assign({}, row);
                dataline.date = row.DATE;
                dataline.theme = row.THEME;
                dataline.name = row.ACTIONNAME;
               
            })
            this.data = fixeddata;
        } else {
            this.error = error;
        }
    }
    
}

==> HTML


<template>
        <div style="height: 300px;">
            <lightning-datatable
                    key-field="id"
                    data={data}
                    columns={columns}>
            </lightning-datatable>
        </div>
<template>

1 Answer 1

0

There are several errors both in Apex method and in JS wired method.

Apex method

Return type mismatch: the method should return a List<Action__c> but you return a List<Plan__c>. If you deployed that class it means that you just removed some code before posting it here, so I'm going to ignore it.
You still have an unnecessary SOQL, the one on Obj__c. Moreover you don't need to pass the result of the SOQL on Plan__c to list contructor: a SOQL will never return null, but if no records are retrieved it will return an empty list.
The method could be rewritten as:

@AuraEnabled(cacheable=true)
public static List<Action__c> GetAction(String id) {

    List<Plan__c> listAction = [SELECT Date__c, Theme__c, Name FROM Plan__c where Obj__c = :id];

    return listAction;
}

JS

In wired method you never push dataline in fixeddata array, so it (and this.data) will be always empty.
Anyway the properties date, theme and name will always be null because row object has no properties named DATE, THEME and ACTIONNAME: since you return an SOBject you have to use the fields api name. Instead of using forEach and a temp array, you could use map():

@wire(GetAction, {id: '$recordId'})
wiredGetAction({ error, data }) {
    if (data) {
        this.data = data.map((record) => {
            return {
                date: record.Date__c,
                theme: record.Theme__c,
                name: record.Name
            };
        });
    } else {
        this.error = error;
    }
}
1
  • thank you so much , your proposition is absolutely correct .
    – ddev96160
    Aug 11, 2022 at 13:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .