We are working with Salesforce Loyalty cloud. In one of our test classes, we are setting up data, where we try to insert some records of the JournalType
standard object coming with Loyalty cloud.
However we are unable to insert any, since we face a DML exception due to DUPLICATE_VALUE
. It is weird since we are not using @TestSetup
nor SeeAllData=true
.
@isTest
private class LM_CalculatePointsTest {
@isTest(SeeAllData=false)
static void SeeAllDataFalseTest() {
List<JournalType> jTypes = [SELECT Id, Name FROM JournalType];
System.debug('Journal Types => ' + JSON.serializePretty(jTypes)); // Output: DEBUG|Journal Types => [ ]
JournalType jt = new JournalType(Name = 'Accrual');
Database.insert(jt); // throws: System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, A journal type with this name already exists.: [Name]
}
}
How is this even possible?
UPDATE:
It seems that it is a SF known issue, which is going to have a fix in the next release.
However, I also spotted other problems:
We have set of unit tests testing functionality around Loyalty Management. We were facing issues recently with execution of these tests and as workaround we were implementing unique names for each LoyaltyProgram
this workaround fixed unit tests if we are executing them one by one in Dev environment. However we are facing issue if we are trying to deploy them to other environment, CI in our case. All tests are failing on :
Internal Salesforce Error: 174353001-109788 (-1847234888)
(-1847234888) Stack Trace: null
Even with dummy deployment validation ( so no real code change is introduced during it ) tests are still failing on Internal Salesforce Error
Curiosity is that if tests are executed in the same environment via console they are passing. But executed in deployment they are failing.
What all failing tests have in common is that they are working with LoyaltyProgram
.
Best Answer
this seems like an issue of Loyalty Management. There is workaround for it, please check: https://trailblazer.salesforce.com/issues_view?id=a1p4V000002aMmsQAE&_ga=2.87831384.1731144651.1655108264-1315708974.1653923732
You need to implement random string (date time in this solution) for name of Loyalty Program. But be super careful implementing it, from my experience you need to create all object manually to make it work. If you are creating your data form static resources you will need to rework it so it will be created from scratch. Note that any update or any kind of change into Loyalty data in runtime of Test will break test with InternalServerError.
For JournalType i would try the same approach with random name.
Salesforce Support confirmed that they have this issue fixed and fix will be released this September