Im attempting to get the recordid from my lighting web component into my apex controller for sorting. Writing a component to display related tasks on the lighting page layout.

Would someone mind helping me with this?


Apex Controller

public with sharing class dataTableLWC {
    @AuraEnabled(cacheable = true)
public static List<Task> fetchTasks(String taskId){

        return [SELECT Id,Subject,ActivityDate FROM TASK WHERE whatid = :taskId];



import {
} from 'lwc';

//import method from the Apex Class
import fetchTasks from '@salesforce/apex/dataTableLWC.fetchTasks';

// Declaring the columns in the datatable
const columns = [{
        label: 'View',
        type: 'button-icon',
        initialWidth: 75,
        typeAttributes: {
            iconName: 'action:preview',
            title: 'Preview',
            variant: 'border-filled',
            alternativeText: 'View'
        label: 'Subject',
        fieldName: 'Subject'
        label: 'Due Date',
        fieldName: 'ActivityDate'

// declare class to expose the component
export default class DataTableComponent extends LightningElement {
    @track columns = columns;
    @track record = {};
    @track rowOffset = 0;
    @track data = {};
    @track bShowModal = false;
    @wire(fetchTasks) parameters;
    @api recordId;

    // Row Action event to show the details of the record
    handleRowAction(event) {
        const row = event.detail.row;
        this.record = row;
        this.bShowModal = true; // display modal window

    // to close modal window set 'bShowModal' tarck value as false
    closeModal() {
        this.bShowModal = false;


       <!-- Detail view modal start -->
     <template if:true={bShowModal}>
      <section role="dialog" tabindex="-1"
              class="slds-modal slds-fade-in-open">
         <div class="slds-modal__container">
            <!-- modal header start -->
            <header class="slds-modal__header">
               <button class="slds-button slds-button_icon slds-modal__close slds-button_icon-inverse" title="Close" onclick={closeModal}>
                  <lightning-icon icon-name="utility:close" alternative-text="close" variant="inverse" size="small" ></lightning-icon>
               <h2 id="modal-heading-01" class="slds-text-heading_medium slds-hyphenate">Record Detail</h2>
            <!-- modal body start -->
            <div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1">
              <dl class="slds-list_horizontal slds-wrap">
                  <dt class="slds-item_label slds-truncate" title="Subject">Subject:</dt>
                  <dd class="slds-item_detail slds-truncate">{record.Subject}</dd>
                  <dt class="slds-item_label slds-truncate" title="DueDate">Due Date:</dt>
                  <dd class="slds-item_detail slds-truncate">{record.ActivityDate}</dd>
            <!-- modal footer start-->
            <footer class="slds-modal__footer">
                 <lightning-button variant="brand"
      <div class="slds-backdrop slds-backdrop_open"></div>
   <!-- Detail view modal end -->


1 Answer 1


You have a line that says

@wire(fetchTasks) parameters;

You need to do more than that. Have a look at the "Wire an Apex Method with a Dynamic Parameter" example in the LWC docs


In their example they're using a phrase 'searchKey' that the user types in, whereas you're going to be using the recordId that is automatically injected into your @api recordID property as long as you're on a record detail page.

You need to make sure your configuration file enables you to put the component on a record detail lightning app page by adding the lightning__RecordPage target in your configuration file for the component.


Specifically here's their Apex class example


public with sharing class ContactController {
        public static List<Contact> findContacts(String searchKey) {
            String key = '%' + searchKey + '%';
            return [SELECT Id, Name, Title, Phone, Email, Picture__c FROM Contact WHERE Name LIKE :key AND Picture__c != null LIMIT 10];

As well as their JS example


import { LightningElement, track, wire } from 'lwc';
import findContacts from '@salesforce/apex/ContactController.findContacts';

/** The delay used when debouncing event handlers before invoking Apex. */
const DELAY = 300;

export default class ApexWireMethodWithParams extends LightningElement {
    @track searchKey = '';

    @wire(findContacts, { searchKey: '$searchKey' })

    handleKeyChange(event) {
        // Debouncing this method: Do not update the reactive property as long as this function is
        // being called within a delay of DELAY. This is to avoid a very large number of Apex method calls.
        const searchKey = event.target.value;
        this.delayTimeout = setTimeout(() => {
            this.searchKey = searchKey;
        }, DELAY);

And here is their HTML template


    <lightning-card title="ApexWireMethodWithParams" icon-name="custom:custom63">
        <div class="slds-m-around_medium">
            <lightning-input type="search" onchange={handleKeyChange} class="slds-m-bottom_small" label="Search" value={searchKey}></lightning-input>
            <template if:true={contacts.data}>
                <template for:each={contacts.data} for:item="contact">
                    <p key={contact.Id}>{contact.Name}</p>
            <template if:true={contacts.error}>
                <c-error-panel errors={contacts.error}></c-error-panel>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.