3

Is there a way to get all Pick list values for a custom field / object using Remote Objects? I have looked at the docs, tried querying the fields but not sure how I can do this. Any guidance or pointers will be appreciated.

3

Yes.

<apex:includeScript value="/soap/ajax/36.0/connection.js"/>
<apex:remoteObjects>
    <apex:remoteObjectModel name="Opportunity" fields="Id,Stage"/>
</apex:remoteObjects>

<script>
var result = sforce.connection.describeSObject("Opportunity");

for (var i = 0; i < result.fields.length; i++) {
    if (result.fields[i].name === 'Stage') {
        var field = result.fields[i];
        for (var j = 0; j < field.picklistValues.length; j++) {
            html += '<option value="' + field.picklistValues[j].value + '">' + field.picklistValues[j].value + '</option>';
        }
    }
}
</script>
  • Thanks @Jesse. This is helpful, and probably the way I will go. However, I read thru Salesforce' docs on VF Remote Objects and was wondering if there is a pure-play Remote Objects based approach. Looks like this is it. Appreciate your help :) – DevIntern Jan 29 '16 at 20:50
  • @DevIntern No prob. Since you are using remote objects you can still create them and use the javascript above to populate them with the field values from your picklist. This is a good reference for the ajax toolkit. – Jesse Milburn Jan 29 '16 at 21:23
3

Overide the behavior of standard retrieve method .Note the below is untested code but highlights the approach

<apex:remoteObjects jsNamespace="$M">
    <apex:remoteObjectModel name="Account" fields="Name,Type,Phone"
        retrieve="{!$RemoteAction.RemoteObjectContactOverride.retrieve}"/>
</apex:remoteObjects>

The apex controller side

public class with sharing RemoteObjectContactOverride {

@RemoteAction
public static Map<String, Object> retrieve(String type, Map<String, Object> fields) {
    System.debug(LoggingLevel.INFO, 'Before calling create on: ' + type);

    // Invoke the standard create action
    // For when you want mostly-normal behavior, with a little something different
    Map<String, Object> result = RemoteObjectController.fetchrecords(type, fields);
    System.debug(LoggingLevel.INFO, 'After calling create on: ' + type);
    System.debug(LoggingLevel.INFO, 'Result: ' + result);

    // Here's the little something different, adding extra data to the result
    //Picklist describe your field 

          List<SelectOption> options = new List<SelectOption>();
          Schema.DescribeFieldResult fieldResult = Account.Type__c.getDescribe();
         List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
         for (Schema.PicklistEntry f: ple) {
           options.add(new SelectOption(f.getLabel(), f.getValue()));
        }     

      //How about serialize here and get JSONresult


    Map<String, Object> customResult = 
        new Map<String, Object> {'custom' => JSONresult };
    customResult.putAll(result);

    return customResult;
  }
}
  • Thanks @Mohith. I guess the answer is between JS Remoting and Ajax Toolkit...Although Salesforce docs have nothing on any limitations around retrieving picklist values via Remote Objects, but it may exist anyway. – DevIntern Jan 29 '16 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.