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
];
}
}