Created
September 19, 2025 02:25
-
-
Save banderson5144/740cd8177c53fc868ef1eb1bc28d31ec to your computer and use it in GitHub Desktop.
dynamic sObject Lookup with GraphQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <!-- https://github.com/pozil/sfdc-ui-lookup-lwc --> | |
| <c-lookup | |
| lwc:ref="sobjLookup" | |
| label="sObject Lookup" | |
| onsearch={handleLookupSearch} | |
| onselectionchange={handleLookupSelectionChange} | |
| ></c-lookup> | |
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { LightningElement, wire, api } from 'lwc'; | |
| import { gql, graphql } from "lightning/uiGraphQLApi" | |
| export default class DynamicLookup extends LightningElement { | |
| @api sobjectApiName; | |
| @api iconName; | |
| searchStr = ''; | |
| @wire(graphql, { | |
| query:"$lookupQuery", | |
| variables: "$variables", | |
| operationName: "sObjectLookup", | |
| }) | |
| graphqlQueryResult({data, errors}){ | |
| if(data){ | |
| const sObjLookup = this.refs.sobjLookup; | |
| const results = data.uiapi.query[this.sobjectApiName].edges.map((edge) => edge.node); | |
| const lookupResults = results.map((rec)=>{ | |
| return { | |
| id: rec.Id, | |
| title: rec.Name.value, | |
| sObjectType: this.sobjectApiName, | |
| icon: this.iconName | |
| } | |
| }); | |
| sObjLookup.setSearchResults(lookupResults); | |
| } | |
| if(errors){ | |
| console.log(errors); | |
| } | |
| } | |
| get variables(){ | |
| return { | |
| searchStr: this.searchStr | |
| }; | |
| } | |
| get lookupQuery(){ | |
| return gql`query sObjectLookup($searchStr: String) { | |
| uiapi { | |
| query { | |
| ${this.sobjectApiName}(first: 5 where: { Name: { like: $searchStr } }){ | |
| edges { | |
| node { | |
| Id | |
| Name{ | |
| value | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| `; | |
| } | |
| handleLookupSearch(event){ | |
| console.log(event); | |
| this.searchStr = event.detail.searchTerm + '%'; | |
| } | |
| handleLookupSelectionChange(event){ | |
| const lookupElement = event.target; | |
| let selectedRecords = lookupElement.getSelection(); | |
| this.searchStr = ''; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> | |
| <apiVersion>64.0</apiVersion> | |
| <isExposed>true</isExposed> | |
| <targets> | |
| <target>lightning__AppPage</target> | |
| </targets> | |
| <targetConfigs> | |
| <targetConfig targets="lightning__AppPage"> | |
| <property name="sobjectApiName" type="String" required="true" default="Contact"/> | |
| <property name="iconName" type="String" required="true" default="standard:contact"/> | |
| </targetConfig> | |
| </targetConfigs> | |
| </LightningComponentBundle> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment