function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Invalid cross reference id on update of custom setting

I have a test method calling a controller method that contains the following code:

My_Custom_Settings__c emailCustomSettings = My_Custom_Settings__c.getOrgDefaults();
if (mycustomSettings == null)
mycustomSettings = new My_Custom_Settings__c(SetupOwnerId=UserInfo.getOrganizationId(), Last_Remove_Date__c=null);
emailcustomSettings.Sf_Instance__c = sfInstance;
upsert emailCustomSettings;

 This code runs without issue in the application but when called from the test method (where the organization settings already exist and an update should occur) it fails with the following errror:

System.DmlException: Update failed. First exception on row 0 with id a03A0000001bfkHIAQ; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []


Interestingly the id it gives in the exception message is a different id to the existing organization settings record...


I've also just discovered that his method works fine if called from a test method in isolation. But it fails when run as part of class containing many test methods some of which call methods that access this custom setting.


Something very strange going on here, possibly a bug?