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
DrawloopSupportDrawloopSupport 

Create or Update Custom Setting Values for User Id or Profile Id via Apex

Does anyone know if it is possible using Apex to create or update a custom setting value specifically for a user Id or profile Id? The Salesforce documentation does not touch this functionality, so I don't know if it is even supported.

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

Right, here goes - 

The SetupOwnerId field on a Hierarhical Custom Setting indicates which User / Profile this setting applies to

 

For a Custom Setting Test, which I want to create for a User u

 

Test__c test = new Test__c(SetupOwnerId = u.Id, Name = 'Ritesh');
insert test;

 

All Answers

Ritesh AswaneyRitesh Aswaney
Yes, hierarchical custom settings make that possible.

Heres a link to a post by the inimitable Jeff Douglas, explaining how
http://blog.jeffdouglas.com/2010/02/08/using-hierarchy-custom-settings-in-salesforce-com/
DrawloopSupportDrawloopSupport

This does not answer my question. I did find this in my search before posting this question. My question is regarding APEX creation or updates to an existing heirarcical custom setting field for a specific user id or profile id. I can successfully create and update a heirarcical custom setting field for the organization, but cannot figure out how to do this for a user or profile.

Ritesh AswaneyRitesh Aswaney
Accessing Custom Settings Programmatically

The cool thing is now you can programmatically access these custom settings based upon the running user and return their most appropriate value (org-wide, profile or user) in formula fields, validation rules, Apex and the Force.com Web Services API with the following interface. Be sure to check out the custom settings docs for more detailed usage.

That's an extract from the article, if you scroll further down. It does talk about accessing value for profiles and users rather than org wide, also has screenshots!
DrawloopSupportDrawloopSupport

I understand how to access the values. How can I CREATE a setting for a specific user or profile? From what I can tell, nothing in this article references that.

Ritesh AswaneyRitesh Aswaney

Right, here goes - 

The SetupOwnerId field on a Hierarhical Custom Setting indicates which User / Profile this setting applies to

 

For a Custom Setting Test, which I want to create for a User u

 

Test__c test = new Test__c(SetupOwnerId = u.Id, Name = 'Ritesh');
insert test;

 

This was selected as the best answer
DrawloopSupportDrawloopSupport

Perfect! Thank you! Perhaps I just missed that on the page... Maybe it could be made clearer in the Salesforce documentation?

DrawloopSupportDrawloopSupport

I double checked, and SetupOwnerId is not mentioned on Jeff Douglass's page nor the Salesforce documentation. Did you just happen to know this or did you find it somewhere?

Ritesh AswaneyRitesh Aswaney

yeah its not on the page. i had a hunch, looked up the object fields in the schema explorer in the force.com ide, created a couple of custom setting rows, and worked my way backwards :)

ISVforce PartnerISVforce Partner

Remember, custom settings are nothing but custom object expect when it comes to caching. So you can use explorer to get field API names and also use DML similar to custom object.