Skip to content

Commit

Permalink
feat: API 56, new methods (#27)
Browse files Browse the repository at this point in the history
* API 56, new methods

* Release please version and config
  • Loading branch information
dschach authored Feb 7, 2023
1 parent 1aa9923 commit cb53adc
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 69 deletions.
6 changes: 5 additions & 1 deletion .forceignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ package.xml
**/.eslintrc.json

# LWC Jest
**/__tests__/**
**/__tests__/**

# Unwanted org metadata to ignore
*.profile
*.settings
4 changes: 2 additions & 2 deletions .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.0.0"
}
".": "1.1.0"
}
62 changes: 60 additions & 2 deletions force-app/main/default/classes/RecordTypes.cls
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
* <br>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
*/
Expand Down Expand Up @@ -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<String, Id>` Map<RecordType Name, RecordType Id>
*/
global static Map<String, Id> getAvailableRecordTypeDevNameIdMap(String sObjectName) {
Map<String, Id> recordTypeMap = new Map<String, Id>();
// 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<String, Schema.RecordTypeInfo> 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<String, Id>` Map<RecordType Name, RecordType Id>
*/
global static Map<String, Id> getAvailableRecordTypeNameIdMap(String sObjectName) {
Map<String, Id> recordTypeMap = new Map<String, Id>();
// 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<String, Schema.RecordTypeInfo> 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<Id, RecordType>` Queried map of Record Types
Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/classes/RecordTypes.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>52.0</apiVersion>
<apiVersion>56.0</apiVersion>
<status>Active</status>
</ApexClass>
82 changes: 46 additions & 36 deletions force-app/main/default/classes/RecordTypesTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @author Evan Callahan
* @since 2010
* @author {@link [David Schach](https://github.com/dschach)}
* @since 2021
* @group RecordTypes
* @see RecordTypes
*/
Expand All @@ -12,43 +13,56 @@ private class RecordTypesTest {
/**
* @description Test various record types and names
* <br>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.'
);
}

/**
Expand All @@ -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<String> rtNames = new Set<String>();
Expand All @@ -116,11 +126,11 @@ private class RecordTypesTest {
resetTest();

List<SelectOption> 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');
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>52.0</apiVersion>
<apiVersion>56.0</apiVersion>
<status>Active</status>
</ApexClass>
24 changes: 12 additions & 12 deletions release-please-config.json
Original file line number Diff line number Diff line change
@@ -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"
}
"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"
}
13 changes: 0 additions & 13 deletions scripts/initialize.sh

This file was deleted.

6 changes: 6 additions & 0 deletions scripts/orginit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion sfdx-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"name": "record-types",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "53.0"
"sourceApiVersion": "56.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<description>Extra metadata to allow 100% coverage when creating a scratch org</description>
<hasActivationRequired>false</hasActivationRequired>
<label>RecordTypes DefaultRT for Testing</label>
<recordTypeVisibilities>
<recordType>Account.Default</recordType>
<visible>true</visible>
</recordTypeVisibilities>
</PermissionSet>

0 comments on commit cb53adc

Please sign in to comment.