|
| 1 | +import type { Record } from '@jsforce/jsforce-node'; |
| 2 | +import { type SalesforceUrlPath, waitForPageErrors } from '../../browserforce.js'; |
| 3 | +import { BrowserforcePlugin } from '../../plugin.js'; |
| 4 | + |
| 5 | +const AVAILABLE_LISTBOX_NAME = 'Available Buttons'; |
| 6 | +const SELECTED_LISTBOX_NAME = 'Selected Buttons'; |
| 7 | +const SAVE_BUTTON_SELECTOR = 'input[name="save"]'; |
| 8 | + |
| 9 | +interface WebLinkRecord extends Record { |
| 10 | + Id: string; |
| 11 | + Name: string; |
| 12 | + NamespacePrefix: string | null; |
| 13 | +} |
| 14 | + |
| 15 | +type ListViewCustomButtonsConfig = { |
| 16 | + objectApiName: string; |
| 17 | + buttons: string[]; |
| 18 | + removeOtherButtons?: boolean | false; |
| 19 | +}; |
| 20 | + |
| 21 | +function buildPagePath(objectApiName: string): SalesforceUrlPath { |
| 22 | + let pageObjApiName = objectApiName; |
| 23 | + if (objectApiName === 'Task' || objectApiName === 'Event') { |
| 24 | + pageObjApiName = 'Activity'; |
| 25 | + } |
| 26 | + |
| 27 | + return `/p/setup/layout/ListButtonsEdit?LayoutEntity=${pageObjApiName}&retURL=${encodeURIComponent('/setup/forcecomHomepage.apexp')}`; |
| 28 | +} |
| 29 | + |
| 30 | +export class ListViewCustomButtons extends BrowserforcePlugin { |
| 31 | + public async retrieve(definition?: ListViewCustomButtonsConfig[]): Promise<ListViewCustomButtonsConfig[]> { |
| 32 | + const results: ListViewCustomButtonsConfig[] = []; |
| 33 | + |
| 34 | + for (const entry of definition) { |
| 35 | + const page = await this.browserforce.openPage(buildPagePath(entry.objectApiName)); |
| 36 | + const selectedListbox = page.getByRole('listbox', { name: SELECTED_LISTBOX_NAME }); |
| 37 | + await selectedListbox.waitFor(); |
| 38 | + |
| 39 | + const buttonIds = ( |
| 40 | + await Promise.all((await selectedListbox.locator('option').all()).map((opt) => opt.getAttribute('value'))) |
| 41 | + ) |
| 42 | + .filter((b) => b?.trim() !== '') |
| 43 | + .map((b) => `'${b?.trim()}'`); |
| 44 | + |
| 45 | + const result = |
| 46 | + buttonIds.length > 0 |
| 47 | + ? await this.browserforce.connection.query<WebLinkRecord>( |
| 48 | + `SELECT Id, Name, NamespacePrefix FROM WebLink WHERE Id IN (${buttonIds.join(',')}) AND PageOrSobjectType = '${entry.objectApiName}'`, |
| 49 | + ) |
| 50 | + : { records: [] }; |
| 51 | + |
| 52 | + const buttons = []; |
| 53 | + |
| 54 | + for (const record of result.records) { |
| 55 | + const fullApiName = record.NamespacePrefix ? `${record.NamespacePrefix}__${record.Name}` : record.Name; |
| 56 | + buttons.push(fullApiName); |
| 57 | + } |
| 58 | + |
| 59 | + results.push({ |
| 60 | + objectApiName: entry.objectApiName, |
| 61 | + buttons: buttons.sort(), |
| 62 | + removeOtherButtons: entry.removeOtherButtons, |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + return results; |
| 67 | + } |
| 68 | + |
| 69 | + public async apply(plan: ListViewCustomButtonsConfig[]): Promise<void> { |
| 70 | + for (const entry of plan) { |
| 71 | + const buttons = await this.queryWebLinks(entry.buttons, entry.objectApiName); |
| 72 | + |
| 73 | + const buttonApiNames = new Set( |
| 74 | + buttons.map((b) => (b.NamespacePrefix ? `${b.NamespacePrefix}__${b.Name}` : b.Name)), |
| 75 | + ); |
| 76 | + const missingButtons = entry.buttons.filter((b) => !buttonApiNames.has(b)); |
| 77 | + |
| 78 | + if (missingButtons.length > 0) { |
| 79 | + this.browserforce.logger?.warn( |
| 80 | + `[${entry.objectApiName}] WebLink(s) not found for button API name(s): ${missingButtons.map((b) => `"${b}"`).join(', ')}. Skipping missing buttons.`, |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + const targetButtonIds = buttons.map((b) => b.Id.slice(0, 15)!); |
| 85 | + |
| 86 | + const page = await this.browserforce.openPage(buildPagePath(entry.objectApiName)); |
| 87 | + const availableListbox = page.getByRole('listbox', { name: AVAILABLE_LISTBOX_NAME }); |
| 88 | + const selectedListbox = page.getByRole('listbox', { name: SELECTED_LISTBOX_NAME }); |
| 89 | + await availableListbox.waitFor(); |
| 90 | + |
| 91 | + const availableButtonIds = ( |
| 92 | + await Promise.all((await availableListbox.locator('option').all()).map((opt) => opt.getAttribute('value'))) |
| 93 | + ).map((b) => b?.trim() ?? ''); |
| 94 | + |
| 95 | + const selectedButtonIds = ( |
| 96 | + await Promise.all((await selectedListbox.locator('option').all()).map((opt) => opt.getAttribute('value'))) |
| 97 | + ) |
| 98 | + .map((b) => b?.trim() ?? '') |
| 99 | + .filter((b) => b !== ''); |
| 100 | + |
| 101 | + const addButton = page.locator('img.rightArrowIcon'); |
| 102 | + const removeButton = page.locator('img.leftArrowIcon'); |
| 103 | + |
| 104 | + for (const buttonId of targetButtonIds) { |
| 105 | + if (availableButtonIds.includes(buttonId) && !selectedButtonIds.includes(buttonId)) { |
| 106 | + await availableListbox.selectOption({ value: buttonId }); |
| 107 | + await addButton.click(); |
| 108 | + await selectedListbox.locator(`option[value="${buttonId}"]`).waitFor(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if (entry.removeOtherButtons) { |
| 113 | + for (const buttonId of selectedButtonIds) { |
| 114 | + if (!targetButtonIds.includes(buttonId)) { |
| 115 | + await selectedListbox.selectOption({ value: buttonId }); |
| 116 | + await removeButton.click(); |
| 117 | + await availableListbox.locator(`option[value="${buttonId}"]`).waitFor(); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + await page.locator(SAVE_BUTTON_SELECTOR).click(); |
| 123 | + await Promise.race([ |
| 124 | + page.waitForURL((url) => url.pathname === '/setup/forcecomHomepage.apexp'), |
| 125 | + waitForPageErrors(page), |
| 126 | + ]); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private async queryWebLinks(apiNames: string[], objectApiName: string): Promise<WebLinkRecord[]> { |
| 131 | + if (!apiNames?.length) { |
| 132 | + return []; |
| 133 | + } |
| 134 | + |
| 135 | + const conditions: string[] = []; |
| 136 | + |
| 137 | + for (const name of apiNames) { |
| 138 | + const parts = name.split('__'); |
| 139 | + if (parts.length > 1) { |
| 140 | + const namespace = parts[0]; |
| 141 | + const devName = parts.slice(1).join('__'); |
| 142 | + conditions.push(`(Name = '${devName}' AND NamespacePrefix = '${namespace}')`); |
| 143 | + } else { |
| 144 | + conditions.push(`(Name = '${name}' AND NamespacePrefix = NULL)`); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + const result = await this.browserforce.connection.query<WebLinkRecord>( |
| 149 | + `SELECT Id, Name, NamespacePrefix FROM WebLink WHERE (${conditions.join(' OR ')}) AND PageOrSobjectType = '${objectApiName}'`, |
| 150 | + ); |
| 151 | + |
| 152 | + return result.records; |
| 153 | + } |
| 154 | +} |
0 commit comments