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
Keith654Keith654 

Custom settings initialization failing on package re-install if custom settings have been managed

If I install my managed beta package into a new developer org the installation works fine.

 

If I install, then uninstall and re-install the installation still works fine.

 

But if I install, use "Manage" to create an org instance of a custom setting object, then uninstall and re-install the re-install fails with errors like these:

 

Apex Classes(01pA0000000i0ux) benefitclaimedactionstest.test()
System.DmlException: Insert failed. First exception on row 0;
first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,
cvc.BenefitTrigger: execution of BeforeInsert

caused by: System.DmlException: Upsert failed. First exception on row 0;
first error: DUPLICATE_VALUE, duplicate value found:
SetupOwnerId duplicates value on record with id: 00DA0000000Y0TO: []

 

It appears the unit tests run as part of the install process are failing because of this underlying code (in this one case only):

 

 

public class CustomSettings {
    public static DateConversionFactors__c getDateConversionFactors() {
        if (DateConversionFactors__c.getOrgDefaults() == null) {
            insert new DateConversionFactors__c(SetupOwnerId = UserInfo.getOrganizationId());
        }
        return DateConversionFactors__c.getOrgDefaults();
    }
}

 

My best guess is that the uninstall is not cleaning up the data created when "Manage" was used completely and so the SetupOwnerId inserted here is being seen as a duplicate.

 

Does anyone have any experience of this problem or insight into how to avoid it?

 

Thanks,

Keith

Abhinav GuptaAbhinav Gupta

Your guess is correct seems the data is not cleaned up or their is a bug in getOrgdefaults().

 

I can suggest two fixes. 

 

1. I never use getOrgDefaults(), instead I use getInstance() to get best matching custom setting. 

2. Try "upsert" instead of "insert" for creating new custom setting. It might be more safer to use.

 

 

public class CustomSettings {
    public static DateConversionFactors__c getDateConversionFactors() {
// GETINSTANCE() used instead of GETORGDEFAULTS()
        if (DateConversionFactors__c.getInstance() == null) {
// UPSERT used instead of INSERT
            upsert new DateConversionFactors__c(SetupOwnerId = UserInfo.getOrganizationId());
        }
        return DateConversionFactors__c.getOrgDefaults();
    }
}

 

Let me know if  you still get the same error

Keith654Keith654

I can confirm that the error still occurs using upsert.

Keith654Keith654

And that using getInstance() doesn't help either though I take your point that its is the more appropriate method to use.

Abhinav GuptaAbhinav Gupta

Another thing that I can suggest depends if your are using hierarchal custom settings. If yes then we can run tests as some other user and do something like this.

 

 

// TEST CODE for creating MOCK User for Apex Tests
User createTestUser() {
		Profile p = [SELECT Id FROM profile WHERE name='System Administrator'];
		User mock = new User(alias = 'testExec', email='testCaseExec@mock.com',
				emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
				localesidkey='en_US', profileid = p.Id,
				timezonesidkey='America/Los_Angeles', username='testexec@mock.com');
		return mock;	
	}

// Use the above test code in System.runAs() to isolate your test data from org data.
static testMethod void testXXXMethod() {
System.runAs(createTestUser()) {
// Your custom settings test code
}
}

 

 

 Now change the code to load custom settings as follows, I just changed the SETUP OWNER ID from ORGID to current USERID

 

public class CustomSettings {
    public static DateConversionFactors__c getDateConversionFactors() {
// GETINSTANCE() used instead of GETORGDEFAULTS()
        if (DateConversionFactors__c.getInstance() == null) {
// UPSERT used instead of INSERT
            upsert new DateConversionFactors__c(SetupOwnerId = UserInfo.getUserId());
        }
        return DateConversionFactors__c.getInstance();
    }
}

 

 

I tried to do an isolation of apex test data from org data. This is normally a good practice to follow. Let me know if it works. System.runAs() for tests is explained in more details here : http://www.tgerm.com/2010/05/systemrunas-501-apex-query-rows.html

 

 

 

elessenger_ctelessenger_ct

I am having this same problem. The Custom Setting object is global, so I am able to test creating the Organization-level object either via my code (which has worked in the past) or through the Salesforce UI. In either case, I get the same error:

 

duplicate value found: SetupOwnerId duplicates value on record with id: 00DA0000000bZCG

Abhinav GuptaAbhinav Gupta

I never got this error. CustomSetting__c.getInstance(), always gives best and finest level of matching custom setting for Org/Profile/User. I suggest checking that for null before doing upsert.

 

 

// try CustomSettings__c.getInstance() for best & lowest level match

if ( CustomSettings__c.getInstance() == null) {
upsert ...;
}

if you want to be super specific try getting instance for the desired SetupOwnerId i.e. User or Profile

if (CustomSettings__c.getInstance('UserId/ProfileId') == null) {

upsert new CustomSettings__c(SetupOwnerId = 'UserId/ProfileId');
}

 

 

 

 

Aiden ByrneAiden Byrne

I'm getting the same problem as reported at the start of the thread.

 

I'm retrieving the custom setting for a specific user via GetInstance and doing an upsert. Is this a known bug? 

 

 WVSavePerUserSettings__c myUserSettings = WVSavePerUserSettings__c.GetInstance(UserInfo.getUserId());

 

        if( myUserSettings == null )
        {
            myUserSettings = new WVSavePerUserSettings__c();
            myUserSettings.setupOwnerId = UserInfo.getUserId();
        }
        
 <change myUserSettings>
         
        upsert myUserSettings;

 

I don't have any problems in my development org. When I create a managed beta, and deploy this, I get the error in the deployed org. I also suspect that the managed beta uninstall is not cleaning up custom data correctly. Even so, I don't know why this code should fail with the same error;

 

Upsert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: SetupOwnerId duplicates value on record with id: 005A0000000hue7: []

FrodoBeutlinFrodoBeutlin

Hi!

 

Sounds like it is fixed, see here: "With the Developer/Config sandbox refreshed, we are not able to create a default value for a hierarchy custom setting. When user tries to create it, it gets the error of Duplicate value found: SetupOwnerId duplicates value on record with id: xxxxxxxx" https://success.salesforce.com/issues_releases_view?release=180008002

aperillataperillat

Im getting the exact same problem. How do you fix it?

soni_9999soni_9999

Same issue on *2* fresh sandboxes with Winter 14. Anyone has a work around? I will be contacting SFDC but I don't think I will get a solution very fast