Skip to content

Commit

Permalink
Merge pull request #373 from amtrack/fix/deactivating-picklist-values…
Browse files Browse the repository at this point in the history
…-error
  • Loading branch information
amtrack authored Jan 12, 2021
2 parents 8855c86 + 185f89d commit 0bd0ae8
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 45 deletions.
12 changes: 12 additions & 0 deletions src/plugins/picklists/index.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,16 @@ describe(Picklists.name, function() {
cmd.output.toString()
);
});
it('should replace and deactivate a picklist value', () => {
const replaceCmd = child.spawnSync(path.resolve('bin', 'run'), [
'browserforce:apply',
'-f',
path.resolve(path.join(__dirname, 'replace-and-deactivate.json'))
]);
assert.deepStrictEqual(replaceCmd.status, 0, replaceCmd.output.toString());
assert(
/changing 'picklistValues' to.*/.test(replaceCmd.output.toString()),
replaceCmd.output.toString()
);
});
});
65 changes: 37 additions & 28 deletions src/plugins/picklists/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,47 @@ export default class Picklists extends BrowserforcePlugin {
const page = await this.browserforce.openPage(picklistUrl);
const picklistPage = new PicklistPage(page);
const values = await picklistPage.getPicklistValues();
const actionRequired = isActionRequired(action, values);
if (actionRequired) {
result.picklistValues.push({
...action,
actionRequired: true
});
} else {
result.picklistValues.push(action);
}
const state = { ...action };
const valueMatch =
action.value !== undefined
? values.find(x => x.value === action.value)
: undefined;
const newValueMatch =
action.newValue !== undefined
? values.find(x => x.value === action.newValue)
: undefined;
state.absent = !valueMatch;
state.active = valueMatch?.active;
state.newValueExists = Boolean(newValueMatch) || action.newValue === null;
result.picklistValues.push(state);
}
return result;
}

public diff(state, definition) {
const actions = definition.picklistValues.filter((target, i) => {
const source = state.picklistValues[i];
if (target.absent) {
return target.absent !== source.absent;
}
if (target.active !== undefined) {
return target.active !== source.active;
}
// replacing a picklist value is not idempotent
if (
source.newValueExists &&
(target.value !== undefined || target.replaceAllBlankValues)
) {
return true;
}
return false;
});
if (actions.length) {
return { picklistValues: actions };
}
return undefined;
}

public async apply(config) {
const conn = this.org.getConnection();
const fileProperties = await listMetadata(
Expand Down Expand Up @@ -104,22 +132,3 @@ async function listMetadata(conn, sobjectTypes) {
return [];
}
}

function isActionRequired(action, values) {
const valueGiven = action.value !== undefined && action.value !== null;
const newValueGiven =
action.newValue !== undefined && action.newValue !== null;
if (valueGiven) {
const match = values.find(x => x.value === action.value);
if (!match) {
return false;
}
if (action.active !== undefined && action.active === match.active) {
return false;
}
}
if (newValueGiven && !values.find(x => x.value === action.newValue)) {
return false;
}
return true;
}
34 changes: 17 additions & 17 deletions src/plugins/picklists/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class PicklistPage {
this.page.waitForNavigation(),
actionLinkHandles[0].click()
]);
await throwPageErrors(this.page);
return new PicklistReplaceAndDeletePage(this.page);
}

Expand Down Expand Up @@ -106,6 +107,7 @@ export class PicklistPage {
this.page.waitForNavigation(),
actionLinkHandles[0].click()
]);
await throwPageErrors(this.page);
return this.page;
}
}
Expand Down Expand Up @@ -145,7 +147,7 @@ export class PicklistReplacePage {
this.page.waitForNavigation(),
this.page.click(this.saveButton)
]);
await this.throwPageErrors();
await throwPageErrors(this.page);
},
{
onFailedAttempt: error => {
Expand All @@ -162,22 +164,6 @@ export class PicklistReplacePage {
}
);
}

async throwPageErrors() {
const errorHandle = await this.page.$(
'div#validationError div.messageText'
);
if (errorHandle) {
const errorMsg = await this.page.evaluate(
(div: HTMLDivElement) => div.innerText,
errorHandle
);
await errorHandle.dispose();
if (errorMsg && errorMsg.trim()) {
throw new Error(errorMsg.trim());
}
}
}
}

export class PicklistReplaceAndDeletePage extends PicklistReplacePage {
Expand All @@ -201,3 +187,17 @@ export class PicklistReplaceAndDeletePage extends PicklistReplacePage {
await this.save();
}
}

async function throwPageErrors(page) {
const errorHandle = await page.$('div#validationError div.messageText');
if (errorHandle) {
const errorMsg = await page.evaluate(
(div: HTMLDivElement) => div.innerText,
errorHandle
);
await errorHandle.dispose();
if (errorMsg && errorMsg.trim()) {
throw new Error(errorMsg.trim());
}
}
}
21 changes: 21 additions & 0 deletions src/plugins/picklists/replace-and-deactivate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "../schema.json",
"settings": {
"picklists": {
"picklistValues": [
{
"metadataType": "CustomField",
"metadataFullName": "Vehicle__c.Features__c",
"value": "AUX",
"newValue": "Media"
},
{
"metadataType": "CustomField",
"metadataFullName": "Vehicle__c.Features__c",
"value": "AUX",
"active": false
}
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
<default>false</default>
<label>Tape Deck</label>
</value>
<value>
<fullName>AUX</fullName>
<default>false</default>
<label>AUX</label>
</value>
</valueSetDefinition>
</valueSet>
</CustomField>

0 comments on commit 0bd0ae8

Please sign in to comment.