From cb53adcd31611235122038eb5f11622daf12b6ca Mon Sep 17 00:00:00 2001 From: David Schach Date: Tue, 7 Feb 2023 15:43:58 -0800 Subject: [PATCH] feat: API 56, new methods (#27) * API 56, new methods * Release please version and config --- .forceignore | 6 +- .release-please-manifest.json | 4 +- .../main/default/classes/RecordTypes.cls | 62 +++++++++++++- .../default/classes/RecordTypes.cls-meta.xml | 2 +- .../main/default/classes/RecordTypesTest.cls | 82 +++++++++++-------- .../classes/RecordTypesTest.cls-meta.xml | 2 +- release-please-config.json | 24 +++--- scripts/initialize.sh | 13 --- scripts/orginit.sh | 6 ++ sfdx-project.json | 2 +- ...faultRT_for_Testing.permissionset-meta.xml | 10 +++ 11 files changed, 144 insertions(+), 69 deletions(-) delete mode 100644 scripts/initialize.sh create mode 100644 unpackaged/main/default/permissionsets/RecordTypes_DefaultRT_for_Testing.permissionset-meta.xml diff --git a/.forceignore b/.forceignore index 0149b5c..5b8fa53 100755 --- a/.forceignore +++ b/.forceignore @@ -11,4 +11,8 @@ package.xml **/.eslintrc.json # LWC Jest -**/__tests__/** \ No newline at end of file +**/__tests__/** + +# Unwanted org metadata to ignore +*.profile +*.settings \ No newline at end of file diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 46b1b67..cddbaef 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "0.0.0" -} \ No newline at end of file + ".": "1.1.0" +} diff --git a/force-app/main/default/classes/RecordTypes.cls b/force-app/main/default/classes/RecordTypes.cls index d10abb9..41016dd 100644 --- a/force-app/main/default/classes/RecordTypes.cls +++ b/force-app/main/default/classes/RecordTypes.cls @@ -17,8 +17,12 @@ *
Usage recommendation: Always use DeveloperName instead of Name, as this is more likely to be * hard-coded in places, while the Name is actually a label and may change (especially due to user language). * - * @author {@link [Evan Callahan](https://github.com/groundwired)}, Groundwire & {@link [David Schach](https://github.com/dschach)}, X-Squared on Demand - * @since 2010, 2021 + * @author {@link [Evan Callahan](https://github.com/groundwired)}, Groundwire + * @since 2010 + * @author {@link [David Schach](https://github.com/dschach)}, X-Squared on Demand + * @since 2021 + * @author Nicolas Vasquez, Globant + * @since 2022 * @group RecordTypes * @see RecordTypesTest */ @@ -353,6 +357,60 @@ global inherited sharing class RecordTypes { return recordTypeMap; } + /** + * @description Gives a map of all ACTIVE `RecordTypeId`s by `developername` for an sObject + * @author Nicolas Vasquez + * @param sObjectName SObject name (with __c if custom, etc) + * @return `Map` Map + */ + global static Map getAvailableRecordTypeDevNameIdMap(String sObjectName) { + Map recordTypeMap = new Map(); + // make sure we have this sObject's record types mapped + if (!recordTypesByName.containsKey(sObjectName)) { + fillMapsForObject(sObjectName); + } + + // fill the name to id map + if (recordTypesByName.containsKey(sObjectName)) { + Map rtMap = recordTypesByName.get(sObjectName); + for (Schema.RecordTypeInfo rti : rtMap.values()) { + if (rti.getName() != 'Master' && rti.isAvailable()) { + // requires permission set to cover in package tests because cannot add record type + // to a profile when creating a scratch org + recordTypeMap.put(rti.getDeveloperName(), rti.getRecordTypeId()); + } + } + } + return recordTypeMap; + } + + /** + * @description Gives a map of all ACTIVE `RecordTypeId`s by name for an sObject + * @author Nicolas Vasquez + * @param sObjectName SObject name (with __c if custom, etc) + * @return `Map` Map + */ + global static Map getAvailableRecordTypeNameIdMap(String sObjectName) { + Map recordTypeMap = new Map(); + // make sure we have this sObject's record types mapped + if (!recordTypesByName.containsKey(sObjectName)) { + fillMapsForObject(sObjectName); + } + + // fill the name to id map + if (recordTypesByName.containsKey(sObjectName)) { + Map rtMap = recordTypesByName.get(sObjectName); + for (Schema.RecordTypeInfo rti : rtMap.values()) { + if (rti.getName() != 'Master' && rti.isAvailable()) { + // requires permission set to cover in package tests because cannot add record type + // to a profile when creating a scratch org + recordTypeMap.put(rti.getName(), rti.getRecordTypeId()); + } + } + } + return recordTypeMap; + } + /** * @description Memoized map of queried `RecordType` records by Id * @return `Map` Queried map of Record Types diff --git a/force-app/main/default/classes/RecordTypes.cls-meta.xml b/force-app/main/default/classes/RecordTypes.cls-meta.xml index f8e5ab6..1248daa 100644 --- a/force-app/main/default/classes/RecordTypes.cls-meta.xml +++ b/force-app/main/default/classes/RecordTypes.cls-meta.xml @@ -1,5 +1,5 @@ - 52.0 + 56.0 Active diff --git a/force-app/main/default/classes/RecordTypesTest.cls b/force-app/main/default/classes/RecordTypesTest.cls index 8f3bcfe..a939231 100644 --- a/force-app/main/default/classes/RecordTypesTest.cls +++ b/force-app/main/default/classes/RecordTypesTest.cls @@ -4,6 +4,7 @@ * @author Evan Callahan * @since 2010 * @author {@link [David Schach](https://github.com/dschach)} + * @since 2021 * @group RecordTypes * @see RecordTypes */ @@ -12,43 +13,56 @@ private class RecordTypesTest { /** * @description Test various record types and names *
We test nonexistent object names, nonexistent record type names, null values, and existing Account record type (if available) + * @author Evan Callahan */ @isTest private static void testRecTypes() { //Test with Bogus object - System.assertEquals(null, RecordTypes.getRecordType('Bogus', null), 'Bogus object should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeDevNameFromId(null), 'Nonexistent RecordType'); - System.assertEquals(null, RecordTypes.getRecordTypeNameFromId(null), 'Nonexistent RecordType'); + Assert.areEqual(null, RecordTypes.getRecordType('Bogus', null), 'Bogus object should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeDevNameFromId(null), 'Nonexistent RecordType'); + Assert.areEqual(null, RecordTypes.getRecordTypeNameFromId(null), 'Nonexistent RecordType'); - System.assertEquals(null, RecordTypes.getRecordTypeIdFromName('Bogust', 'Bogus'), 'Bogus object should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeDevNameFromName('Bogust', 'Bogus'), 'Bogus object should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeIdFromName('Bogust', 'Bogus'), 'Bogus object should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeDevNameFromName('Bogust', 'Bogus'), 'Bogus object should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeNameFromDevName('Bogus', 'Bogus'), 'Bogus object should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeIdFromDevName('Bogus', 'Bogus'), 'Bogus object should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeNameFromDevName('Bogus', 'Bogus'), 'Bogus object should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeIdFromDevName('Bogus', 'Bogus'), 'Bogus object should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeNameFromId('Bogus', null), 'Bogus object should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeDevNameFromId('bogus', null), 'Bogus object should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeNameFromId('Bogus', null), 'Bogus object should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeDevNameFromId('bogus', null), 'Bogus object should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeIdFromDevName('Bogus', 'Bogus'), 'Bogus object should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeIdFromName('Bogus', 'Bogus'), 'Bogus object should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeIdFromDevName('Bogus', 'Bogus'), 'Bogus object should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeIdFromName('Bogus', 'Bogus'), 'Bogus object should not exist'); + + Assert.isTrue(RecordTypes.getAvailableRecordTypeDevNameIdMap('Bogus').isEmpty(), 'Bogus object should not exist'); + Assert.isTrue(RecordTypes.getAvailableRecordTypeNameIdMap('Bogus').isEmpty(), 'Bogus object should not exist'); + + Assert.isTrue(RecordTypes.getRecordTypeDevNameIdMap('Bogus').isEmpty(), 'Bogus object should not exist'); + Assert.isTrue(RecordTypes.getRecordTypeNameIdMap('Bogus').isEmpty(), 'Bogus object should not exist'); //Test with Account - System.assertEquals(null, RecordTypes.getRecordTypeIdFromName('Account', 'Bogus'), 'Bogus record type should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeIdFromName('Account', 'Bogus'), 'Bogus record type should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeDevNameFromName('Account', 'Bogus'), 'Bogus record type should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeDevNameFromName('Account', 'Bogus'), 'Bogus record type should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeNameFromDevName('Account', 'Bogus'), 'Bogus record type should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeIdFromDevName('Account', 'Bogus'), 'Bogus record type should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeNameFromDevName('Account', 'Bogus'), 'Bogus record type should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeIdFromDevName('Account', 'Bogus'), 'Bogus record type should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeNameFromId('Account', null), 'Null record type should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeDevNameFromId('Account', null), 'Null record type should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeNameFromId('Account', null), 'Null record type should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeDevNameFromId('Account', null), 'Null record type should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeIdFromDevName('Account', 'Bogus'), 'Bogus record type should not exist'); - System.assertEquals(null, RecordTypes.getRecordTypeIdFromName('Account', 'Bogus'), 'Bogus record type should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeIdFromDevName('Account', 'Bogus'), 'Bogus record type should not exist'); + Assert.areEqual(null, RecordTypes.getRecordTypeIdFromName('Account', 'Bogus'), 'Bogus record type should not exist'); - System.assert(RecordTypes.getRecordTypeDevNameIdMap('Bogus').isEmpty(), 'Bogus object should not exist'); - System.assert(RecordTypes.getRecordTypeNameIdMap('Bogus').isEmpty(), 'Bogus object should not exist'); + Assert.isNotNull( + RecordTypes.getAvailableRecordTypeDevNameIdMap('Account'), + 'Should return at least an empty map, and populated one if we have record types for Account.' + ); + Assert.isNotNull( + RecordTYpes.getAvailableRecordTypeNameIdMap('Account'), + 'Should return at least an empty map, and populated one if we have record types for Account.' + ); } /** @@ -70,36 +84,32 @@ private class RecordTypesTest { Schema.RecordTypeInfo rti = RecordTypes.getRecordType('Account', activeRT.Id); - System.assertNotEquals(null, RecordTypes.getRecordTypeDevNameFromId(activeRT.Id), 'We should have had an Account RecordType'); - System.assertNotEquals(null, RecordTypes.getRecordTypeNameFromId(activeRT.Id), 'We should have had an Account RecordType'); + Assert.areNotEqual(null, RecordTypes.getRecordTypeDevNameFromId(activeRT.Id), 'We should have had an Account RecordType'); + Assert.areNotEqual(null, RecordTypes.getRecordTypeNameFromId(activeRT.Id), 'We should have had an Account RecordType'); - System.assertEquals( + Assert.areEqual( activeRT.Id, RecordTypes.getRecordTypeIdFromName('Account', activeRt.Name), 'Submitted ' + activeRT.Name + ' and expected an Id to be returned: ' + activeRT.Id ); - System.assertEquals( + Assert.areEqual( rti.getRecordTypeId(), RecordTypes.getRecordTypeIdFromName('Account', activeRt.Name), 'Submitted ' + activeRT.Name + ' and expected an Id: ' + rti.getRecordTypeId() ); resetTest(); - System.assertEquals( - activeRT.DeveloperName, - RecordTypes.getRecordTypeDevNameFromName('Account', activeRT.Name), - 'Error - mismatch on returned developername' - ); + Assert.areEqual(activeRT.DeveloperName, RecordTypes.getRecordTypeDevNameFromName('Account', activeRT.Name), 'Error - mismatch on returned developername'); resetTest(); - System.assertEquals(activeRT.Name, RecordTypes.getRecordTypeNameFromDevName('Account', activeRT.DeveloperName), 'Error - mismatch on returned name'); - System.assertEquals(activeRT.Id, RecordTypes.getRecordTypeIdFromDevName('Account', activeRT.DeveloperName), 'Error - mismatch on returned Id'); + Assert.areEqual(activeRT.Name, RecordTypes.getRecordTypeNameFromDevName('Account', activeRT.DeveloperName), 'Error - mismatch on returned name'); + Assert.areEqual(activeRT.Id, RecordTypes.getRecordTypeIdFromDevName('Account', activeRT.DeveloperName), 'Error - mismatch on returned Id'); resetTest(); - System.assertEquals(activeRT.Name, RecordTypes.getRecordTypeNameFromId('Account', activeRT.Id), 'Error - mismatch on returned name'); + Assert.areEqual(activeRT.Name, RecordTypes.getRecordTypeNameFromId('Account', activeRT.Id), 'Error - mismatch on returned name'); resetTest(); - System.assertEquals(activeRT.DeveloperName, RecordTypes.getRecordTypeDevNameFromId('Account', activeRT.Id), 'Error - mismatch on returned developername'); + Assert.areEqual(activeRT.DeveloperName, RecordTypes.getRecordTypeDevNameFromId('Account', activeRT.Id), 'Error - mismatch on returned developername'); resetTest(); Set rtNames = new Set(); @@ -116,11 +126,11 @@ private class RecordTypesTest { resetTest(); List rtOptions = RecordTypes.getRecordTypesForSelectList('Account'); - System.assertEquals(accountRecordTypes.size(), rtOptions.size(), 'Error - mismatch on record type count'); + Assert.areEqual(accountRecordTypes.size(), rtOptions.size(), 'Error - mismatch on record type count'); resetTest(); rtOptions = RecordTypes.getStringRecordTypesForSelectList('Account'); - System.assertEquals(accountRecordTypes.size(), rtOptions.size(), 'Error - mismatch on record type count'); + Assert.areEqual(accountRecordTypes.size(), rtOptions.size(), 'Error - mismatch on record type count'); } /** diff --git a/force-app/main/default/classes/RecordTypesTest.cls-meta.xml b/force-app/main/default/classes/RecordTypesTest.cls-meta.xml index f8e5ab6..1248daa 100644 --- a/force-app/main/default/classes/RecordTypesTest.cls-meta.xml +++ b/force-app/main/default/classes/RecordTypesTest.cls-meta.xml @@ -1,5 +1,5 @@ - 52.0 + 56.0 Active diff --git a/release-please-config.json b/release-please-config.json index ad4d3c4..e300a97 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -1,13 +1,13 @@ { - "packages": { - ".": { - "changelog-path": "CHANGELOG.md", - "release-type": "salesforce", - "bump-minor-pre-major": false, - "bump-patch-for-minor-pre-major": false, - "draft": false, - "prerelease": false - } - }, - "$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json" -} \ No newline at end of file + "packages": { + ".": { + "changelog-path": "CHANGELOG.md", + "release-type": "salesforce", + "bump-minor-pre-major": false, + "bump-patch-for-minor-pre-major": false, + "draft": false, + "prerelease": false + } + }, + "$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json" +} diff --git a/scripts/initialize.sh b/scripts/initialize.sh deleted file mode 100644 index 0214162..0000000 --- a/scripts/initialize.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -sfdx update -npm install -npm update -npm install --save-dev eslint-plugin-prettier -npm install --save-dev prettier prettier-plugin-apex -npm install --save-dev @salesforce/eslint-config-lwc@latest @lwc/eslint-plugin-lwc@latest @salesforce/eslint-plugin-lightning@latest @salesforce/eslint-plugin-aura@latest eslint-plugin-import eslint-plugin-jest -npm install --save-dev eslint-config-prettier - -# upgrade all packages to latest versions -npm update -npm ci \ No newline at end of file diff --git a/scripts/orginit.sh b/scripts/orginit.sh index 3192acb..4cd512e 100644 --- a/scripts/orginit.sh +++ b/scripts/orginit.sh @@ -9,6 +9,12 @@ sfdx force:org:create -f config/project-scratch-def.json --durationdays 10 -a Re echo "Pushing metadata" sfdx force:source:push +echo "Assigning permission set" +sfdx force:user:permset:assign -n unpackaged/main/default/permissionsets/RecordTypes_DefaultRT_for_Testing + +echo "Running Apex tests" +sfdx force:apex:test:run --codecoverage --resultformat human -l RunLocalTests --wait 20 + echo "Opening org" sfdx force:org:open diff --git a/sfdx-project.json b/sfdx-project.json index 64ded08..65622bd 100644 --- a/sfdx-project.json +++ b/sfdx-project.json @@ -8,5 +8,5 @@ "name": "record-types", "namespace": "", "sfdcLoginUrl": "https://login.salesforce.com", - "sourceApiVersion": "53.0" + "sourceApiVersion": "56.0" } diff --git a/unpackaged/main/default/permissionsets/RecordTypes_DefaultRT_for_Testing.permissionset-meta.xml b/unpackaged/main/default/permissionsets/RecordTypes_DefaultRT_for_Testing.permissionset-meta.xml new file mode 100644 index 0000000..26a7452 --- /dev/null +++ b/unpackaged/main/default/permissionsets/RecordTypes_DefaultRT_for_Testing.permissionset-meta.xml @@ -0,0 +1,10 @@ + + + Extra metadata to allow 100% coverage when creating a scratch org + false + + + Account.Default + true + +