4

Is it possible to include these fields Id,Name,Description__c in a Remote Objects definition within a Visualforce Page without hardcoding them? I would like to pass them from elsewhere and am exploring how to do that.

<apex:remoteObjects >
<apex:remoteObjectModel jsShortHand="rObjModelJsSH" name="customObject__c" fields="Id,Name,Description__c">            
    <apex:remoteObjectField name="relationship__c" jsShorthand="Parent" />
</apex:remoteObjectModel>

0

1 Answer 1

4

The obvious way to try this is to set the fields list to a Visualforce expression. Unfortunately, although that does compile, it doesn't work:

<apex:remoteObjects jsNamespace="Test">
    <apex:remoteObjectModel name="Account" fields="{! fieldList }">            
    </apex:remoteObjectModel>
</apex:remoteObjects>

You get back the Id only when you perform a retrieve.

However, you can do this with a remote objects action override.

Visualforce Page

<apex:page controller="TestRemoteActionOverrideController">
    <apex:remoteObjects jsNamespace="Test">
        <apex:remoteObjectModel name="Account" fields="Id"
                                retrieve="{! $RemoteAction.TestRemoteActionOverrideController.retrieve }" />            
    </apex:remoteObjects>
    <script>
        var act = new Test.Account();
        act.retrieve(
            { },
            function(error, results, event) {
                alert('Got results: ' + JSON.stringify(results) + ' Error was: ' + error);
            }
        );
    </script>
</apex:page>

Apex Controller

public class TestRemoteActionOverrideController{
    @RemoteAction
    public static Map<String,Object> retrieve(String type, List<String> fields, Map<String, Object> criteria) {
        fields.add('Name');

        // call through to the default remote object controller with our modified field list.
        return RemoteObjectController.retrieve(type, fields, criteria);
    }
}

If you load this page, you'll find both the Name and Id fields populated in the alert message. You can, if necessary, override other remote actions accordingly.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .