Skip to content

Commit

Permalink
Working with payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasparrague committed Jun 19, 2024
1 parent 3240ef9 commit 9fb0ebe
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 0 deletions.
103 changes: 103 additions & 0 deletions src-lookup-sample/main/default/classes/AccountContactLookup.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
public class AccountContactLookup implements LookupResult.ILookupResult {
public final static Map<String, Object> ACCOUNT_ICON = new Map<String, Object>{
'iconName' => 'standard:contact'
};

public List<LookupResult> getSelection(
List<String> selectedIds,
Map<String, Object> payload
) {

String accountId = (String) payload.get('accountId');
List<LookupResult> result = new List<LookupResult>();

if (accountId == null) {
return result;
}

List<Contact> contacts = [
SELECT Name, Id, Phone
FROM Contact
WHERE AccountId = :accountId
AND Id IN: selectedIds
];

for (Contact contact : contacts) {
result.add(buildResult(contact));
}

return result;
}

public List<LookupResult> getSearchResults(
String searchTerm,
List<String> selectedIds,
Map<String, Object> payload
) {

String accountId = (String) payload.get('accountId');
List<LookupResult> result = new List<LookupResult>();

if (accountId == null) {
return result;
}

searchTerm = '%' + searchTerm + '%';

List<Contact> contacts = [
SELECT Name, Email, Phone
FROM Contact
WHERE
(Name LIKE :searchTerm
OR Phone LIKE :searchTerm
OR Email LIKE :searchTerm)
AND AccountId = :accountId
AND Id NOT IN :selectedIds
];

for (Contact contact : contacts) {
result.add(buildResult(contact));
}

return result;
}

public List<LookupResult> getDefaultSearchResults(
Map<String, Object> payload
) {

String accountId = (String) payload.get('accountId');
List<LookupResult> result = new List<LookupResult>();

if (accountId == null) {
return result;
}

List<Contact> contacts = [
SELECT Name, id, Phone
FROM Contact
WHERE AccountId = :accountId
LIMIT 10
];

for (Contact contact : contacts) {
result.add(buildResult(contact));
}

return result;
}

private LookupResult buildResult(Contact contact) {
LookupResult result = new LookupResult();
result.id = contact.Id;
result.icon = ACCOUNT_ICON;
result.title = contact.Name;
result.recordPayload = new Map<String, Object> {
'name' => contact.Name,
'phone' => contact.Phone
};

return result;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>
63 changes: 63 additions & 0 deletions src-lookup-sample/main/default/classes/AccountLookup.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
public class AccountLookup implements LookupResult.ILookupResult {

public final static Map<String, Object> ACCOUNT_ICON = new Map<String, Object>{
'iconName' => 'standard:account'
};

public List<LookupResult> getSelection(List<String> selectedIds, Map<String, Object> payload) {
List<LookupResult> result = new List<LookupResult>();

for (Account account : [SELECT Id, Name FROM Account WHERE Id IN: selectedIds]) {

result.add(buildResult(account));
}

return result;
}

public List<LookupResult> getSearchResults(
String searchTerm,
List<String> selectedIds,
Map<String, Object> payload) {

List<LookupResult> result = new List<LookupResult>();

searchTerm = '%' + searchTerm + '%';

for (Account account : [
SELECT Id, Name
FROM Account
WHERE
Name LIKE :searchTerm
AND Id NOT IN :selectedIds
]) {
result.add(buildResult(account));
}

return result;
}

public List<LookupResult> getDefaultSearchResults(Map<String, Object> payload) {
List<LookupResult> result = new List<LookupResult>();

for (Account account : [SELECT Id, Name FROM Account LIMIT 10]) {
result.add(buildResult(account));
System.debug(account);
}

return result;
}

private LookupResult buildResult(Account account) {
LookupResult result = new LookupResult();
result.id = account.Id;
result.icon = ACCOUNT_ICON;
result.title = account.Name;
result.recordPayload = new Map<String, Object> {
'type' => 'Contact',
'name' => account.Name
};

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<template>
<lightning-tabset variant="scoped">
<lightning-tab label='Working with payloads'>
<lightning-card
title="Working with payloads"
>
<div class="slds-form slds-form_stacked slds-var-m-around_xx-large">
<p class="slds-var-m-vertical_large">
<strong>Note:</strong> select an account to start
</p>
<c-lookup
lwc:ref="lookup"
unique-id="AccountLookup"
label="Account Search"
onchange={handleAccountChange}
value={accountValue}
required
></c-lookup>
<p>{accountText}</p>
<c-lookup
lwc:ref="lookup"
unique-id="AccountContactLookup"
label="Contact Search"
value={contactValue}
payload={contactPayload}
disabled={accountValueEmpty}
onchange={handleContactChange}
required
></c-lookup>
<p>{contactText}</p>
</div>
</lightning-card>
</lightning-tab>
</lightning-tabset>
</template>

39 changes: 39 additions & 0 deletions src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { LightningElement, track } from "lwc";

Check warning on line 1 in src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js

View check run for this annotation

Codecov / codecov/patch

src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js#L1

Added line #L1 was not covered by tests
import { NavigationMixin } from "lightning/navigation";

export default class LookupAccount extends NavigationMixin( LightningElement ) {

Check warning on line 4 in src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js

View check run for this annotation

Codecov / codecov/patch

src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js#L4

Added line #L4 was not covered by tests

@track contactPayload;
accountValue;
contactValue;
accountText;

Check warning on line 9 in src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js

View check run for this annotation

Codecov / codecov/patch

src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js#L6-L9

Added lines #L6 - L9 were not covered by tests
contactText;


handleAccountChange(event) {
this.contactValue = [];
this.accountText = "";
this.contactText = "";
this.accountValue = event.detail.value[0];
this.contactPayload = { accountId : event.detail.value[0] };

Check warning on line 18 in src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js

View check run for this annotation

Codecov / codecov/patch

src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js#L13-L18

Added lines #L13 - L18 were not covered by tests

if (this.accountValue !== undefined) {
const account = JSON.parse(JSON.stringify(event.detail.payload[0].recordPayload));
this.accountText = `Your account selected was ${account.name}`;

Check warning on line 22 in src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js

View check run for this annotation

Codecov / codecov/patch

src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js#L21-L22

Added lines #L21 - L22 were not covered by tests
}
}

handleContactChange(event) {
this.contactText = "";
this.contactValue = event.detail.value[0];

Check warning on line 28 in src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js

View check run for this annotation

Codecov / codecov/patch

src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js#L26-L28

Added lines #L26 - L28 were not covered by tests

if (this.accountValue === undefined || this.contactValue !== undefined) {
const contact = JSON.parse(JSON.stringify(event.detail.payload[0].recordPayload));
this.contactText = `Your contact selected was ${contact.name} and its phone number is ${contact.phone}`;

Check warning on line 32 in src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js

View check run for this annotation

Codecov / codecov/patch

src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js#L31-L32

Added lines #L31 - L32 were not covered by tests
}
}

get accountValueEmpty() {
return !this.accountValue;

Check warning on line 37 in src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js

View check run for this annotation

Codecov / codecov/patch

src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js#L36-L37

Added lines #L36 - L37 were not covered by tests
}
}

Check warning on line 39 in src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js

View check run for this annotation

Codecov / codecov/patch

src-lookup-sample/main/default/lwc/lookupAccount/lookupAccount.js#L39

Added line #L39 was not covered by tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>59.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
</targets>
</LightningComponentBundle>

0 comments on commit 9fb0ebe

Please sign in to comment.