Skip to content

Commit

Permalink
Merge pull request #222 from cesarParra/fix-picklist-with-single-valu…
Browse files Browse the repository at this point in the history
…e-parsing

Fixes issue when parsing a picklist custom field that has a single va…
  • Loading branch information
cesarParra authored Nov 26, 2024
2 parents 39c2a30 + 9e84cbc commit ee2244e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cparra/apexdocs",
"version": "3.7.0",
"version": "3.7.1",
"description": "Library with CLI capabilities to generate documentation for Salesforce Apex classes.",
"keywords": [
"apex",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('when parsing custom field metadata', () => {
assertEither(result, (data) => expect(data[0].type.description).toBe('A Photo URL field'));
});

test('can parse picklist values', async () => {
test('can parse picklist values when there are multiple picklist values available', async () => {
const unparsed: UnparsedCustomFieldBundle = {
type: 'customfield',
name: 'Status__c',
Expand Down Expand Up @@ -153,6 +153,73 @@ describe('when parsing custom field metadata', () => {
assertEither(result, (data) => expect(data[0].type.pickListValues).toEqual(['Staging', 'Active', 'Inactive']));
});

test('can parse picklist values when there is a single value available', async () => {
const unparsed: UnparsedCustomFieldBundle = {
type: 'customfield',
name: 'Status__c',
parentName: 'MyFirstObject__c',
filePath: 'src/field/Status__c.field-meta.xml',
content: `
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Status__c</fullName>
<externalId>false</externalId>
<label>Status</label>
<required>true</required>
<trackFeedHistory>false</trackFeedHistory>
<description>Status</description>
<type>Picklist</type>
<valueSet>
<restricted>true</restricted>
<valueSetDefinition>
<sorted>false</sorted>
<value>
<fullName>Staging</fullName>
<default>false</default>
<label>Staging</label>
</value>
</valueSetDefinition>
</valueSet>
</CustomField>`,
};

const result = await reflectCustomFieldSources([unparsed])();

assertEither(result, (data) => expect(data[0].type.description).toBe('Status'));
assertEither(result, (data) => expect(data[0].type.pickListValues).toEqual(['Staging']));
});

test('can parse picklist values when there are no values', async () => {
const unparsed: UnparsedCustomFieldBundle = {
type: 'customfield',
name: 'Status__c',
parentName: 'MyFirstObject__c',
filePath: 'src/field/Status__c.field-meta.xml',
content: `
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Status__c</fullName>
<externalId>false</externalId>
<label>Status</label>
<required>true</required>
<trackFeedHistory>false</trackFeedHistory>
<description>Status</description>
<type>Picklist</type>
<valueSet>
<restricted>true</restricted>
<valueSetDefinition>
<sorted>false</sorted>
</valueSetDefinition>
</valueSet>
</CustomField>`,
};

const result = await reflectCustomFieldSources([unparsed])();

assertEither(result, (data) => expect(data[0].type.description).toBe('Status'));
assertEither(result, (data) => expect(data[0].type.pickListValues).toEqual(undefined));
});

test('An error is returned when the XML is in an invalid format', async () => {
const unparsed: UnparsedCustomFieldBundle = {
type: 'customfield',
Expand Down
14 changes: 12 additions & 2 deletions src/core/reflection/sobject/parse-picklist-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@ function toPickListValues(customField: MaybeTyped): string[] | undefined {
if ('valueSetDefinition' in valueSet) {
const valueSetDefinition = valueSet.valueSetDefinition as object;
if ('value' in valueSetDefinition) {
const pickListValues = valueSetDefinition.value as Record<'fullName', string>[];
return pickListValues.filter((each) => 'fullName' in each).map((current) => current.fullName);
const pickListValues = valueSetDefinition.value as Record<'fullName', string>[] | Record<'fullName', string>;
if (isArrayOfValues(pickListValues)) {
return pickListValues.filter((each) => 'fullName' in each).map((current) => current.fullName);
} else {
return [pickListValues.fullName];
}
}
}
}

return undefined;
}

function isArrayOfValues(
value: Record<string, unknown> | Record<string, unknown>[],
): value is Record<string, unknown>[] {
return Array.isArray(value);
}

0 comments on commit ee2244e

Please sign in to comment.