So I have a some java script where I want to pass a list of Objects from a controller and get a number of fields. However, I'm having issues passing the individual variables.

However, I'd rather call the controller (I think it's better form)


public List<Activity__C> getActs(){
        myActs = [select Task_Site__c, Activity_Date__c, Activity_Time_Stamp__c,GeoCode__Latitude__s, GeoCode__Longitude__s, workday__r.owner.name
                  from Activity__c 
                  where workday__r.ownerid =: curUser and Activity_Date__c =: filter ];        
        return myActs;


From my controller how can I access the Task_Site__C and other fields via Javascript?

<script type="text/javascript">

        var result = new Array();
        result = '{!Acts}';
        //sforce.connection.sessionId  = "{!$Api.Session_ID}";    
        //sforce.connection.query('select Task_Site__c, Activity_Date__c, Activity_Time_Stamp__c,GeoCode__Latitude__s, GeoCode__Longitude__s, workday__r.owner.name from Activity__c');

        for (var i=0; i < result.length; i++)
  • I disagree that querying in your controller is any better than using sforce.connection. They wouldn't have opened up the API if they didn't want people using it. – Adrian Larson Jul 1 '16 at 20:47
  • really, seems like all of the logic is then written in the javascript? – tjcinnamon Jul 1 '16 at 20:53
  • 1
    Yeah I don't see the problem. In fact it seems to be the direction the platform is moving. A bit more dangerous because they don't force you to unit test, but still an acceptable trade-off for performance improvements. Maybe if you get really complicated, but for one query it's much simpler to keep it in one place. Otherwise use serialization. – Adrian Larson Jul 1 '16 at 20:55

My advice is to keep the query in Javascript. Much simpler to keep all the logic in one place than try to route server logic into client code when you can avoid it.

You can, however, use a serialized array property.

public String mySerializedList { get; private set; }
public MyConstructor()
    mySerializedList = JSON.serialize([/*some query*/]);

Then in your client-side application you can parse it.

var objectArray = JSON.parse('{!JSINHTMLENCODE(mySerializedList)}');
  • Sweet! Thanks. I'm new to MVC so I'm trying to use those standards – tjcinnamon Jul 2 '16 at 1:38

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.