I am looking for a way to limit the contact lookup field to only contacts that are associated with that account. I need the same thing for the asset, but what I get for contact I can make work for the asset.
So basic HTML structure:
<lightning-record-edit-form recor-id={recordId}
<div class="slds-grid slds-gutters">
<div class="slds-col" style="width:100%">
<lightning-input-field field-name="AccountId" required>
</lightning-input-field>
</div>
<div class="slds-col" style="width:100%">
<lightning-input-field field-name="AssetId" required>
</lightning-input-field>
</div>
</div>
<div class="slds-grid slds-gutters">
<div class="slds-col" style="width:100%">
<template if:false={showAddContact}>
<lightning-input-field field-name="ContactId" required>
</lightning-input-field>
</template>
</div>
</div>
How do I get the contact lookup to only show what is associated with that account?
--------------------- UPDATED --------------------------
LookupController Class I have attempted to put in the AccountId but nothing seems to work with the variable. If I hardcode the contacts get pulled up.
The second part of this is I can't figure out how to pass in the new array of contacts to a lookup component so the user can search out of those specific contacts.
public with sharing class LookupController {
@AuraEnabled(cacheable=true)
public static List<Object> getRecordsByName(String objectName, String searchFor, String accountId) {
String queryString = 'SELECT Id, Name FROM ';
queryString += String.escapeSingleQuotes(objectName) + ' WHERE Name Like \'%' + String.escapeSingleQuotes(searchFor) + '%\'' + ' AND AccountId = \'' + String.escapeSingleQuotes(accountId) + '\'';
return Database.query(queryString);
}
}
JS Code
@wire(getContactRecords, { searchKey: '$searchKey', accountId: '$accountId'})
contacts;
handleOnLoad(event) {
if (this.loadedForm == false) {
this.accountId = this.record.data.fields.AccountId.value;
if (this.accountId != null) {
this.contact = {Name : event.detail.Name, Id: event.detail.Id, accountId: this.accountId}
} else {
// Do nothing
}
}
}