A drop down list from a custom setting

  • 1
  • Question
  • Updated 4 years ago
  • Answered
Zack answered a previous question on custom settings. I can see them in SKUID and create a model from one.... but the field I want to apply the values in the custom setting to (custom setting is a list of disabilities) is just a text field in the participant_disability object.... so when i make a table for the participant disabilities, the "disability" field does not give me the option of setting up a drop down list... i guess it has to be a "lookup" type field to do that

So how can i use my custom setting ?
Photo of ktyler

ktyler

  • 9,244 Points 5k badge 2x thumb

Posted 5 years ago

  • 1
Photo of Anna Wiersema

Anna Wiersema

  • 10,890 Points 10k badge 2x thumb
I am a bit confused. What previous question are you talking about? By custom setting, are you referring to custom object?

It sounds like, for this to work, Disability does need to be changed to a lookup field. Because there's no way (that I know of) to have a list like that for text fields .
Photo of Anna Wiersema

Anna Wiersema

  • 10,890 Points 10k badge 2x thumb
I was thinking about this question again and wondering if it would be more effective to just make disabilities a custom object instead of trying to get the list from the custom setting. Then you would be able to do a lookup and everything.
(Edited)
Photo of J.

J., Official Rep

  • 7,470 Points 5k badge 2x thumb
If I'm understanding the data structure of your Disabilities Custom Setting correctly, you should be able to do this via a snippet. For instance, here's what an inline snippet version might look like:


var field = arguments[0];
var value = arguments[1];

if (field.mode === 'edit') {
// Build the Custom Setting options for the PICKLIST
var disabilityOptions = [];
skuid.$.each(skuid.model.getModel('Disabilities').getRows(), function(i,row) {
disabilityOptions.push({
value : row.Name, // Will be stored in the "Participant_Disability" object
label : row.Name // Will display in the PICKLIST
});
});

// Render the Custom Setting options as a PICKLIST
var disabilitySelect = skuid.ui.renderers.PICKLIST.edit({
entries : disabilityOptions,
required : false,
value : value
}).change(function() {
// Update the row in the Custom Object
field.model.updateRow(field.row,'Disability__c',skuid.$(this).val());
});

// Append the PICKLIST to the DOM element
field.element.append(disabilitySelect);
} else {
// If the mode is anything other than edit, display the field as Text
skuid.ui.fieldRenderers.TEXT[field.mode](field,value);
}


You'll probably need to modify this to fit your actual Custom Objects and Custom Settings, but does that help?

For reference, I think that the previous question which Zach answered may be found here.
Photo of ktyler

ktyler

  • 9,244 Points 5k badge 2x thumb
J
That seems to make sense
I modified the line that references the model to say "Disability" instead of "disabilities"...the model has the field "name" in it
the name of the field that the data is to be entered into is "Disability__c"

but when I preview the form the that should hold the select is empty
Photo of J.

J., Official Rep

  • 7,470 Points 5k badge 2x thumb
That snippet code as I pasted it above only works as an Inline Snippet:



After adding it there, you would just put the Snippet Name in the Render Snippet box shown in your screenshot:



Now, if you are going to be referencing this same snippet across multiple pages, you may want to put in a Static Resource instead of just copying/pasting the same Inline Snippet. To do that, add the following around the snippet code and drop it into a static resource:

skuid.snippet.registerSnippet( "DisabilitiesList", function() {

//
// *** Snippet code goes here *** //
//
});


You would then include that static resource in each Skuid page that needed the snippet, and call the snippet by name in "Render Snippet" box as previously mentioned:



Also, we automatically include a Static Resource of [Whatever the name of the page's module is]JS with each Skuid page. If all the pages that will use this drop down are in the same module, create the appropriately named static resource (if it doesn't already exist), add the snippet to it (including the "registerSnippet" wrapper), add then call the snippet where needed. Modules are optional on pages though, so this may or may not apply.

For a great explanation of the different types of Javascript includes (including Snippets and Static Resources), check out this post from Zach.
Photo of ktyler

ktyler

  • 9,244 Points 5k badge 2x thumb
Bingo,
That worked.
Salesforce seems to be encouraging the use of custom settings... it seems like you could make this part of your interface by making custom setting an option for a text field so the person would not have to add a snippet for each field... in the case of this form I have about 10 of them to add now. It would also make it a lot easier for those not as well versed in javascript
Photo of ktyler

ktyler

  • 9,244 Points 5k badge 2x thumb
actually, it would be nice if this script could be called in a generic way

is there some way i can pass in the name of the field for this to be applied to, and the name of the model to get the list from ?
I think the value is called "name" in all the custom settings

it looks like these 2 things are just strings in this code... can i pass them as arguments[2] and arguments[3]
Photo of J.

J., Official Rep

  • 7,470 Points 5k badge 2x thumb
There isn't a way to send additional arguments to a snippet, but you can use snippets to send arguments to another JavaScript function. If you abstract that initial function, then you could call it from as many snippets as needed, changing the arguments each time.

If you've got 10 Custom Setting Select Boxes to add, I'd definitely recommend going this route. If they are all on one page, a JavaScript resource of location "In-Line" would work fine. If they are spread across multiple pages, then I'd recommend using a Static Resource.
Photo of ktyler

ktyler

  • 9,244 Points 5k badge 2x thumb
so you're saying i can call a generic function inside the snippet, pass it field, value, model name etc

would i have access to the skuid object inside the generic function ?
Photo of J.

J., Official Rep

  • 7,470 Points 5k badge 2x thumb
Yes to both. You can call plain old JavaScript functions from within snippets, and the skuid object should be accessible as well. Something like this:

var makeCustomSettingList = function (field, value, listModelName, listRowValueField, listRowLabelField, updateFieldName) {

//
// This would be the abstracted version of that original script.
//
}

skuid.snippet.registerSnippet('DisabilitiesList', function(field, value) {
makeCustomSettingList(field, value, 'Disability', 'Name', 'Name', 'Disability__c');
});

skuid.snippet.registerSnippet('SomethingElseList', function(field, value) {
makeCustomSettingList(field, value, 'SomethingElse', 'Name', 'Name', 'Something__c');
});


Put the code in an inline resource (if only used on a single page) or a Static Resource (if used across multiple pages), and then call each snippet by name in custom renderers as needed. Does that make sense?
Photo of ktyler

ktyler

  • 9,244 Points 5k badge 2x thumb
Ben,
That seemed clear
I made the function generic, and added the snippets that call it.
One question is... does the snippet name in the text box on the skuid form and the snippet name in the "registerSnippet" function have to be the same name ?

When i preview the page I get "object is not a function"
I did upload the function file as a static resource in salesforce.
Photo of J.

J., Official Rep

  • 7,470 Points 5k badge 2x thumb
Ken,

No, those two names can be different. The string in the "Resource Name" box is to identify the chunk of JavaScript. The string that is the first argument of the "registerSnippet" function identifies the actual snippet. This way, you can put all of those snippets into a single In-Line Resource, and name the resource something more categorical like "CustomSettingSnippets" rather than having 10 different Resources which each contain a single snippet.

On the "object is not a function" error, I think that you need to change this...

value : row(listRowValueField),

label : row(listRowLabelField)


...to this...

value : row[listRowValueField],

label : row[listRowLabelField]


...in your "makeCustomSettingList" function (square brackets instead of parenthesis).
Photo of ktyler

ktyler

  • 9,244 Points 5k badge 2x thumb
Ben,
OK, that time everything worked. I think this would be a very useful technique. I'm going to try and write it up on my SKUID blog... maybe it will be one of the sections in the SKUID book.
Thanks for your patient help.
Photo of ktyler

ktyler

  • 9,244 Points 5k badge 2x thumb
I've turned this into a set of instructions for using Custom Settings to power lookup lists on a SKUID page and published it on my blog.
http://skuidink.kennethtyler.com/2013...
Photo of Jarrod Hinson

Jarrod Hinson

  • 2,310 Points 2k badge 2x thumb
Is this still the best way to do this?
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
I believe it still is.