Skip to content

Commit

Permalink
Working with payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasparrague committed Jul 22, 2024
1 parent 3240ef9 commit 4d55a02
Show file tree
Hide file tree
Showing 14 changed files with 422 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ The lookup component provides the following features:
2. **Create the `LookupDefinition` record**
Go to Setup > Custom Metadata Types > LookupDefinition > add a new record
Go to Setup > Custom Metadata Types > click in Manage Records (LookupDefinition) > add a new record
**Label**: `String`<br/>
**ApiName**: `String`<br/>
Expand Down Expand Up @@ -143,6 +143,7 @@ public List<LookupResult> getDefaultSearchResults(Map<String, Object> payload) {
return result;
}
```
**RECOMMENDED**: `selectedIds` *is the Id selected, use in `getSelection()` method and `getSearchResults()` method*

## Work with contexts using `payload`

Expand Down Expand Up @@ -170,7 +171,8 @@ singleLookupResult.recordPayload = new Map<String, Object> {
```js
handleChange(event) {
const value = event.detail.value;// ["someId", "someId2", "someId3"]
const payload = event.detail.payload;// {"someId": {type: nurse, name : johana}, someId2: {ty...}}
const payload = { someId : event.detail.value };// {"someId": {type: nurse, name : johana}, someId2: {ty...}}
//this create a copy of the payload of the id consulted
// TODO: do something with the selection
}
```
Expand Down
98 changes: 98 additions & 0 deletions src-lookup-sample/main/default/classes/AccountContactLookup.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
public class AccountContactLookup implements LookupResult.ILookupResult {

public final static Map<String, Object> CONTACT_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 = CONTACT_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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@IsTest
private class AccountContactLookupTest {
@IsTest
static void shouldCreateLookupResults() {
// DATA
Account account = new Account(Name = 'account name test');
insert account;

Contact contact = new Contact(
FirstName = 'FirstName',
LastName = 'LastName',
Phone = '123456789',
Email = '[email protected]',
HasOptedOutOfEmail = false,
AccountId = account.Id
);
insert contact;

Map<String, Object> payload = new Map<String, Object>();
payload.put('accountId', account.Id);

// TEST
Test.startTest();
AccountContactLookup accountContactLookup = new AccountContactLookup();
List<LookupResult> selectionResult = AccountContactLookup.getSelection(
new List<String>{ contact.id },
payload
);
List<LookupResult> defaultSearchResultsResult = AccountContactLookup.getDefaultSearchResults(
payload
);
List<LookupResult> searchResultsResult = AccountContactLookup.getSearchResults(
'123',
new List<String>(),
payload
);
Test.stopTest();

// ASSERT
Assert.areEqual(
selectionResult[0].id,
contact.id,
'should get the selection'
);
Assert.areEqual(
defaultSearchResultsResult[0].id,
contact.id,
'should set getDefaultResults from recentlyViewed'
);
Assert.areEqual(
searchResultsResult[0].id,
contact.id,
'filters contacts using searchKey'
);
}
}
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>61.0</apiVersion>
<status>Active</status>
</ApexClass>
62 changes: 62 additions & 0 deletions src-lookup-sample/main/default/classes/AccountLookup.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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));
}

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> {
'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>
46 changes: 46 additions & 0 deletions src-lookup-sample/main/default/classes/AccountLookupTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@IsTest
private class AccountLookupTest {
@IsTest
static void shouldCreateLookupResults() {
// DATA
Account account = new Account(Name = 'account name test');
insert account;

Map<String, Object> payload = new Map<String, Object>();
payload.put('name', account.Name);

// TEST
Test.startTest();
AccountLookup accountLookup = new AccountLookup();
List<LookupResult> selectionResult = AccountLookup.getSelection(
new List<String>{ account.Id },
payload
);
List<LookupResult> defaultSearchResultsResult = AccountLookup.getDefaultSearchResults(
payload
);
List<LookupResult> searchResultsResult = AccountLookup.getSearchResults(
'accou',
new List<String>(),
payload
);
Test.stopTest();

// ASSERT
Assert.areEqual(
selectionResult[0].Id,
account.Id,
'should get the selection'
);
Assert.areEqual(
defaultSearchResultsResult[0].Id,
account.Id,
'should set getDefaultResults from recentlyViewed'
);
Assert.areEqual(
searchResultsResult[0].Id,
account.Id,
'filters contacts using searchKey'
);
}
}
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>61.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { createElement } from 'lwc';
import WorkingWithPayloads from 'c/workingWithPayloads';

describe('c-working-with-payloads', () => {
afterEach(() => {
// The jsdom instance is shared across test cases in a single file so reset the DOM
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});

it('this should show a text with account name when selected the account and contact name and phone when selected contact', async () => {
// Arrange
const element = createElement('c-working-with-payloads', {
is: WorkingWithPayloads
});

document.body.appendChild(element);

const accountChangeEvent = new CustomEvent('change', {
detail: {
value: ['accountId'],
payload: {
'accountId': { name: 'Account name' }
}
}
});

const contactChangeEvent = new CustomEvent('change', {
detail: {
value: ['contactId'],
payload: {
'contactId': { name: 'Contact Name', phone: 'Phone' }
}
}
});

// Act
const accountLookup = element.shadowRoot.querySelector('c-lookup[data-id="accountLookup"]');
accountLookup.dispatchEvent(accountChangeEvent);

const contactLookup = element.shadowRoot.querySelector('c-lookup[data-id="contactLookup"]');
contactLookup.dispatchEvent(contactChangeEvent);

await Promise.resolve();
// Assert
const selectedAccount = element.shadowRoot.querySelector('p[data-id="selectedAccount"]');
const selectedContact = element.shadowRoot.querySelector('p[data-id="selectedContact"]');
expect(selectedAccount.textContent).toBe('Your account selected was Account name');
expect(selectedContact.textContent).toBe('Your contact selected was Contact Name and its phone number is Phone');
});
});
Loading

0 comments on commit 4d55a02

Please sign in to comment.