1

I have a custom Lookup component that will within another LWC. I need to retrieve the filter information for any lookups that has an active filter. I was able to retrieve data using getFilteredLookupInfo() and filteredlookupinfo. If the new custom lookup component results is based off a value that is selected on the original LWC. What information do I need from the filter to populate the new lookup component?
For example the field metadata is this:

 <filterItems>
        <field>$Source.Account__c</field>
        <operation>equals</operation>
        <valueField>BrokerAccount__c.Account__c</valueField>
    </filterItems>

Do I need to be able to get the valueField? Thanks, P

1
  • David, using the filteredlookupinfo.getControllingFields() caused the component not to load. Also using filteredlookupinfo.dependent or filteredlookupinfo.controllingfields or filteredlookupinfo.isDependent() caused the component not to load. I am able to retrieve information using just getFilteredLookupInfo() or filteredlookupinfo().
    – Phuc
    Nov 30, 2020 at 14:37

1 Answer 1

1

You can try using the tooling API and implementing the API in apex to achieve the same.

Check the details of the API here

https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/tooling_api_objects_lookupfilter.htm

Here is the draft code to get you started, Note the below

  • It's not production-ready code.
  • Add Error Handling and Unit Tests
  • LWC Code is not shown and I focus on only backend code
  • I have simplified authentication using vf page. I recommend you look into Connected Apps and Named Credential to make sure you are securely authenticating.

Here we go with detailed code

  1. Create a VF page to get you the session, since with LWC Session we do not allow you to access the Tooling API

    <apex:page contentType="application/json">
      {!$Api.Session_ID}
    </apex:page>
    
  2. Create a Class to read SessionID from vf and expose as Utility to other apex class

    public inherited sharing class SessionHelper {
     public static String getSessionId() {
      return Test.isRunningTest()
         ? ''
         : Page.session.getContent().toString().trim();
      }
    }
    
  3. Create an API client to make outbound apex HTTP callout

     public inherited sharing class APIClient {
      public static String makeAPICall(String endPoint, String method) {
     try {
         Http http = new Http();
         HttpRequest request = new HttpRequest();
         request.setHeader(
             'Authorization',
             'Bearer ' + SessionHelper.getSessionId()
         );
         request.setTimeout(60000);
         request.setEndpoint(endPoint);
         request.setMethod(method);
         HttpResponse response = http.send(request);
         return response.getBody();
     } catch (Exception ex) {
         system.debug('Exception in tooling API Call:' + ex.getMessage());
         return ex.getMessage();
        }
      }
    }
    
  4. Create an apex class to fetch the LookupFilter Metadata

    public with sharing class LookupFilterController {
    
      static String toolingSOQL(string queryString) {
       Double apiVersion = getApiVersion();
       String path = '/tooling/query/?q=' + queryString.replace(' ', '+');
        String endpointurl =
         Url.getOrgDomainUrl().toExternalForm() +
         '/services/data/v' +
         apiVersion +
         path;
        return APIClient.makeAPICall(endpointurl, 'GET');
     }
    
     static Double getApiVersion() {
       return [
         SELECT ApiVersion
         FROM ApexClass
         ORDER BY ApiVersion DESC
         LIMIT 1
       ]
       .ApiVersion;
     }
    
     @AuraEnabled(cacheable=true)
     public static string getLookupFilters(String developerName) {
          string toolingsoql =
             'SELECT FullName,' +
             'DeveloperName,' +
             'SourceObject,' +
             'Metadata'
             'FROM LookupFilter ' +
             'WHERE DeveloperName=\'' +
              developerName +
             '\'';
        return toolingSOQL(toolingsoql);
       }
     }
    

I have not thoroughly tested the above code but I am hoping it unblocks you!

5
  • Mohith, I tested where i ran a query in dev console against lookupFilter but can I do this in a class? Also, how do I query against the FilterItem Metadata? Thank you
    – Phuc
    Nov 30, 2020 at 14:43
  • I will produce a more detailed apex code once I get sometime today? Thank you! Nov 30, 2020 at 14:44
  • Thanks Mohith, hopefully thit will get me closer to fully resolving my issue,
    – Phuc
    Nov 30, 2020 at 17:20
  • @Phuc Added code to help you! Dec 1, 2020 at 1:52
  • Thank you Mohith
    – Phuc
    Dec 3, 2020 at 19:12

You must log in to answer this question.

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