+ Start a Discussion
Gerardo Rodriguez LaraGerardo Rodriguez Lara 

RemoteObject Javascript

Hello, I would know in my code how can I get the name of the user who edited the post. I can get ID from the User that modified the post, but I want to get the name.

<!-- Remote Objects definition to set accessible sObjects and fields -->
    <apex:remoteObjects >
        <apex:remoteObjectModel name="Post__c" jsShorthand="Post" fields="Name,Id,LastModifiedDate, LastModifiedById">
            <apex:remoteObjectField name="Body__c" jsShorthand="Body"/>
            <apex:remoteObjectField name="Tags__c" jsShorthand="Tags"/>
        </apex:remoteObjectModel>
    </apex:remoteObjects>

<div id="rows"> </div>

<script>
           // Create a new Remote Object
            var pt = new SObjectModel.Post();

            // Use the Remote Object to query for 3 post records
            pt.retrieve({ limit: 3 }, function(err, records){
                if(err) alert(err.message);
                else {
                             var rows = document.getElementById("rows");
                             var ptLastModifiedById  = record.get("LastModifiedById");
                             //div data
                             var postData = document.createElement('div');
                             postData.className = "postData";
                             postData.innerHTML += ptLastModifiedDate + " by " + ptLastModifiedById;
                             div.appendChild(postData);

                             rows.appendChild(div);
                       });
                  }
            });
</script>

Best Answer chosen by Gerardo Rodriguez Lara
@Karanraj@Karanraj
Visualforce remote object is currently not supported for the cross-object field. You can't use LastModifiedby.Name in the javascript code.
There are two approaches you can able to solve this problem
  • Create one more remote object model for the user object in your visualforce page and create separate javascript function query the user object field values based on the Id. Check this blog post for sample code - http://20xdeveloper.com/2015/06/14/retrieving-parent-visualforce-remote-objects/
  • Create a new text-based formula field in the Post__c object to get display the lastmodifedby name. Now you can directly use that formula field in your javascript code instead of LastModifiedByID    

All Answers

@Karanraj@Karanraj
Visualforce remote object is currently not supported for the cross-object field. You can't use LastModifiedby.Name in the javascript code.
There are two approaches you can able to solve this problem
  • Create one more remote object model for the user object in your visualforce page and create separate javascript function query the user object field values based on the Id. Check this blog post for sample code - http://20xdeveloper.com/2015/06/14/retrieving-parent-visualforce-remote-objects/
  • Create a new text-based formula field in the Post__c object to get display the lastmodifedby name. Now you can directly use that formula field in your javascript code instead of LastModifiedByID    
This was selected as the best answer
James LoghryJames Loghry
Also, when you post code, whether Apex or Visualforce or JavaScript, on these developer boards, please use the code formatting button (< >).  Thanks!