How to populate ui only picklist field through apex code.

  • 2
  • Question
  • Updated 6 months ago
  • Answered
Photo of Rahul Agarwal

Rahul Agarwal

  • 102 Points 100 badge 2x thumb

Posted 6 months ago

  • 2
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
Rahul,

You may not need to use Apex.  If the data that drives the pick list is in an object, you can drive the pick list options using a model in Skuid.  Here is a posting where I provided a sample page that shows you how to do this:

https://community.skuid.com/skuid/topics/how-to-bind-skuid-picklist-ui-field-from-snippet

If you still want to go down the path of using an Apex class, then you can Salesforce's JS library to call an Apex Class directly.  The function call is 'sforce.apex.execute'.  Here is a nice write up from Zach on what you need to do:

https://community.skuid.com/skuid/topics/calling_apex_function

Here is an example of a snippet to build and render a custom pick list values on a UI only field:

https://community.skuid.com/skuid/topics/ui-only-picklist-custom-field-renderer-snippet-stopped-work...


I would recommend that you consider how you can setup an object to act as a source for your pick list options.  This will keep your page declarative and greatly reduce what you have to maintain going forward.

Thanks,

Bill
Photo of Rahul Agarwal

Rahul Agarwal

  • 102 Points 100 badge 2x thumb
Thanks you very much Bill for your reply.
I have to use an apex class as the picklist values are based on some condition and dynamic as well.
But as per the code below I am not able to populate the values. Can you please help me out.

var params = arguments[0],
$ = skuid.$;
var productRow = skuid.model.getModel('GeneralProduct').getFirstRow();
var applicationModel = skuid.model.getModel('GeneralApplication');


var appModel = skuid.model.getModel('CommonApplicationModel');
var appRow = appModel.data[0];
var ret = sforce.apex.execute("apexClass","apexMethod",{AppId:appRow.Id});
var test = applicationModel.getField('Team_users');

   var raw= appModel.getFirstRow();

var field = arguments[0];
var result = {
    'genesis__Applications__c' : applicationModel,
};

return result;

var raw= appModel.getFirstRow();
 var UserType=raw.Team_users;
 
 var picklistEntries = [];

picklistEntries = element.metadata.picklistEntries;
picklistEntries.length = 0;  

for(var a=0;a<ret.length;a++){
            picklistEntries.push( { value:ret[a].Name , label:ret[a].Name, defaultValue: false, active: true  });
        }
skuid.ui.fieldRenderers[element.metadata.displaytype][element.mode](element,value);
field.metadata.picklistEntries = picklistEntries;
(Edited)
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
Rahul,

The line "return result;' will stop the execution of your script at that line.  The remaining code that actually adds the pick list entries will never run.  Also, in your second to last line (skuid.ui.fieldRenderers[element.metadata.displaytype][element.mode](element,value);), you reference 'element' which you have not defined--this should be 'field' instead of 'element'.

There also seems to be some extra code that is not needed to render the picklist field.  I would remove this code.  Here is what I would recommend you use as your custom render Snippet:
var field = arguments[0],
$ = skuid.$;

var ret = sforce.apex.execute("apexClass","apexMethod",{AppId:appRow.Id});

var picklistEntries = field.metadata.picklistEntries;
picklistEntries.length = 0;  

for(var a=0;a<ret.length;a++){
            picklistEntries.push( { value:ret[a].Name , label:ret[a].Name, defaultValue: false, active: true  });
        }

skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);

Thanks,

Bill
Photo of Rahul Agarwal

Rahul Agarwal

  • 102 Points 100 badge 2x thumb
Thank you so much Bill for your reply.
My code is failing in line var picklistEntries = field.metadata.picklistEntries;
I checked by using alert and nothing is coming up. Can you please help
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
Rahul,

Try adding a pick list entry to your UI only field.  Also make sure that your UI only field is display type 'picklist'.

Thanks,

Bill