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
NBlasgenNBlasgen 

Via the API, How do you see Custom Settings?

I have a custom setting that I want to be able to see the value of via the API.

"Custom settings that have Privacy defined as Public are exposed to the API in the same way custom objects are exposed."

But how do I match that to the user I'm logged in as?  It's not like the Id's returned are the same as ProfileIds.

 

PS: Sorry for the double posting of this.

Message Edited by NBlasgen on 01-24-2010 09:43 PM
Best Answer chosen by Admin (Salesforce Developers) 
NBlasgenNBlasgen

SELECT <whatever> FROM <custom_setting_object> WHERE (SetupOwnerId = '<current_profile_id>' OR (NOT Name LIKE '%Profile%')) AND IsDeleted = False

 

That query will return either 1 or 2 rows.  If 1 row, then it's simply the organizational default.  If two rows, it's the user's profile settings.  In either case, the first row returned is the only row anyone will care about.  If the user doesn't have a profile setting, it will be the organization default.  If they do, the first row will be the profile information. ... at least this is what my testing has resulted in.

Message Edited by NBlasgen on 02-08-2010 01:16 PM

All Answers

werewolfwerewolf

Are you accessing it from within Apex or via the external API? 

 

My first inclination would be to think that for user-level custom settings, the external API would show it as if it were a custom object, but only the rows pertaining to the logged-in user would be shown.  Is that not the case?

BlasgenBlasgen

M,

 

API, SOQL:  I get two rows back.  I haven't tried it with multiple accounts though to see if the results change, but I was surprised by the multiple rows.  Just wish the Salesforce documents on Custom Settings were a little more complete with examples for API as they are with Apex.  Shouldn't be hard for them to provide a SOQL example, eh?

NBlasgenNBlasgen

SELECT <whatever> FROM <custom_setting_object> WHERE (SetupOwnerId = '<current_profile_id>' OR (NOT Name LIKE '%Profile%')) AND IsDeleted = False

 

That query will return either 1 or 2 rows.  If 1 row, then it's simply the organizational default.  If two rows, it's the user's profile settings.  In either case, the first row returned is the only row anyone will care about.  If the user doesn't have a profile setting, it will be the organization default.  If they do, the first row will be the profile information. ... at least this is what my testing has resulted in.

Message Edited by NBlasgen on 02-08-2010 01:16 PM
This was selected as the best answer
SuperfellSuperfell
You have no order by clause, so you shouldn't read anything into the ordering of the results.
AlexPHPAlexPHP

Has anyone figured out a solution to this issue?

 

I'm still confused and am unable to match User/Profile level Hierarchy Custom Settings to the Users/Profiles that I have set them up for via API.

 

When I try to query with "SetupOwnerId" condition, it just says "No such column 'SetupOwnerId' on entity..."

 

I suppose I could create another Custom Settings custom field that labels everything, but it seems like there should be a better way.

gene.koopmangene.koopman

You're almost there, but there were a few missing pieces. Here's the complete query:

 

SELECT <fields> FROM <custom setting api name> WHERE (SetupOwnerId = '<user ID>' OR SetupOwnerID = '<user profile ID>' OR (NOT Name like '%<custom setting name>%')) AND IsDeleted = False 

Here's the difference

 

  1. SetupOwnerID can contain the ID to either a user or profile. This is because a hierarchical custom setting can contain specific entries for either a user or a profile. Thus you need to query for both, as there's no guarantee your user fits into either or both.
  2. The Name will contain either a GUID value, which is of interest insofar as it indicates that the record contains the org defaults, or the name of the custom setting appended with a space and either '(User)' or '(Profile)'. If the record is an entry for a user, it will end with '(User)', if it's a profile then it will end with '(Profile)'. 

Consequently, the above code will return between 1 and 3 values. If there is a user specific entry as well as a profile specific entry for the profile of the user used for the query, there will be 3. If just a user specific entry, there will be 2. If there are no user specific entries or profile entries, only 1: the defaults.

 

You can write code to parse the returns. However, if you're only interested in the returns which are most pertinent to the user you're querying for, use this:

 

SELECT <fields> FROM <custom setting api name> WHERE (SetupOwnerId = '<user ID>' OR SetupOwnerID = '<user profile ID>' OR (NOT Name like '%<custom setting name>%')) AND IsDeleted = False order by name desc LIMIT 1

By ordering the results thusly, the User specific entry is brought to the top. if there is no user specific entry, but there is a profile specific entry, it is brought to the top. If neither of these are available, there is only 1 row to return: the default. As you're only returning the top row, whichever row is brought to the top is the one you'll receive. 

 

Hope this helps.