0

I have been following this article to try and retrieve data for a custom object's related data. Basically, I have a Student__c object, and I have a StudentCourseRelationship__c object. the StudentCourseRelationship__c object has a master-detail lookup to Student__c. What I am attempting to do is write a component that, when on a Student Page, will display all of the student's courses they are related to (yes, I know this is kind of built in, but down the road I will need data that traverses across many relationships).

So, here is what I have in my component's JS file:

import { LightningElement, api, wire} from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import getCourseData from '@salesforce/apex/StudentCourseData.getStudentCourseData';

export default class StudentCourseRelationship extends LightningElement {
    @api recordId;

    // COURSES
    @wire(getCourseData, { studentId: '$recordId' }) courseRels;

    get courseCount() {
        return this.courseRels.data;
    }
}

And here is my APEX code for the getCourseData method:

public with sharing class StudentCourseData {
    public static List<StudentCourseRelationship__c> getStudentCourseData(String studentId) {
        return [SELECT Id, Student__c, Course__c FROM StudentCourseRelationship__c WHERE Student__c = :studentId];
    }
}

Finally, here is my .html code where the data is displayed:

<template>
    <lightning-card title="Courses" icon-name="custom:custom14">
        <div class="slds-m-around_medium">
            <p>{recordId}</p>
            <template if:true={courseRels.data}>
                <p>COURSES</p>
                <ui>
                    <template for:each={courseRels.data} for:item="rel">
                        <li key={rel.Id}>{rel.Course__c}</li>
                    </template>
                    </ui>
            </template>
            <template if:false={courseRels.data}>
                <p>NO COURSES TO DISPLAY</p>
            </template>
        </div>
    </lightning-card>
</template>

This seems to be correct by the link I provided above, but for all students I am getting zero relationships even though I have verified that this particular student I am viewing has 6 StudentCourseRelationship__c relationships..

Is this the proper way to pull data in a web component, or am I doing something wrong that I am not noticing?

5
  • Is this your exact code? The reason I ask is because I don't see @AuraEnabled annotation on the apex method.
    – Jayant Das
    Feb 20, 2019 at 20:41
  • Yes, is AuraEnabled required? What is that I guess?
    – BlondeSwan
    Feb 20, 2019 at 21:03
  • Yes it is required, that's a pre-requisite. Take a look at the documentation as how to call the apex methods. Your issue at the first instance seems to be related to that.
    – Jayant Das
    Feb 20, 2019 at 21:04
  • If you want to make that an answer, I'll accept it.
    – BlondeSwan
    Feb 20, 2019 at 21:14
  • I was actually typing the answer when you commented :). That's your issue, and once you fix it, it should work.
    – Jayant Das
    Feb 20, 2019 at 21:16

1 Answer 1

1

You're missing @AuraEnabled annotation in your apex method.

For an apex method to be invoked from LWC (or a Lighting Aura Component), you will need to annotate the method with this annotation. You can look for more details on the documentation.

Your method should look like:

@AuraEnabled(cacheable=true)
public static List<StudentCourseRelationship__c> getStudentCourseData(String studentId) {
    ...
}

You must log in to answer this question.

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