1

Account Product looks up to Account. I want to get account id from current account product and pass it to my controller and return a query using the account id. I am trying to achieve this with LWC. I am getting the account id (using @wire) and passing it to a function (using @wire). I am not able to figure the right way to pass the parameter in "@wire(getOpportunityList, { searchKey: accID() })" returned from "record". Is this the right way to do?

JS

import { LightningElement, wire, api } from 'lwc';
import getOpportunityList from '@salesforce/apex/whitespaceController.getOpportunityList';
import { getRecord } from 'lightning/uiRecordApi';
import ACCOUNT_ID_FIELD from '@salesforce/schema/Account_Product__c.Account__c';

const fields = [ACCOUNT_ID_FIELD];

export default class WhiteSpaceOpp extends LightningElement {
    opps;
    error;
    @api recordId;    
    @wire(getRecord, { recordId: '$recordId', fields })
    record;

    get accID() {
        return getFieldValue(this.record.data, ACCOUNT_ID_FIELD);
    }

    
    @wire(getOpportunityList, { searchKey: accID() })
    wiredContacts({ error, data }) {
        if (data) {
            this.opps = data;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.opps = undefined;
        }
    }
}

Controller

public with sharing class whitespaceController {
    
    @AuraEnabled(cacheable=true)
    public static List<Opportunity> getOpportunityList(String searchKey) {
        set<ID> oppSet = new set<ID>();
      
        for(opportunitylineItem oli : [Select id,opportunity.id from opportunitylineItem where Opportunity.Accountid = :searchKey and product2.Whitespace_Category__c = 'XYZ']){
            oppSet.add(oli.opportunity.id);
        }

        return [
            SELECT Id, Name, CloseDate, StageName, Stage_Changed_Date__c
            FROM Opportunity where id in :oppSet         
            WITH SECURITY_ENFORCED
            LIMIT 10
        ];
    }
}





3 Answers 3

1

The wire method runs whenever the input parameter is modified.

//declare a variable to hold accountId
parentAccountId='';
@wire(getRecord, { recordId: '$recordId', fields })
record({error,data}){
  if(data){
    this.parentAccountId = data.Account__c;
    // this will invoke the other wire method.
  }
}

@wire(getOpportunityList, { searchKey: '$parentAccountId' })
    wiredContacts({ error, data }) {
        if (data) {
            this.opps = data;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.opps = undefined;
        }
    }
0

You want to bind the value to an actual property, and in the wire method, in order to declare it as dynamic, you need to wrap it with single quotes and add a dollar sign before the property name.

4
  • Tried this --> @wire(getOpportunityList, { searchKey: '$accID()' }) got this error --> Property property of MemberExpression expected node to be of a type [\"Identifier\",\"PrivateName\"] but instead got \"StringLiteral\"",
    – user102769
    Commented Aug 17, 2021 at 2:20
  • because you are using a getter, not a property
    – glls
    Commented Aug 17, 2021 at 3:49
  • So how should i write it?
    – user102769
    Commented Aug 17, 2021 at 4:21
  • the answer is pretty straightforward, dont use a getter as a dynamic property for an attribute. use a property...
    – glls
    Commented Aug 17, 2021 at 15:55
0

Instead of getter, please use wire methods and assign the value of account Id to a variable.

parentAccountId;
@wire(getRecord, { recordId: '$recordId', fields })
record({ error, data }) {
    if(data){
        this.accountId = getFieldValue(data, ACCOUNT_ID_FIELD);
    }
    
@wire(getOpportunityList, { searchKey: '$parentAccountId' })
wiredContacts({ error, data }) {
    if (data) {
        this.opps = data;
        this.error = undefined;
    } else if (error) {
        this.error = error;
        this.opps = undefined;
    }
}

You must log in to answer this question.

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