Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I have two simple apex methods in my controller:

 public String getStringOne() {
    String returnStringOne = 'this is the string';
    return returnStringOne;
}

 public String getStringTwo() {
    String returnStringTwo = 'this is the other string';
    return returnStringTwo;
}

And I have an array in my js file:

var myArray = [];

How do I pass the results of both of these methods into my array?

['this is the string', 'this is the other string']
share|improve this question
2  
myArray.push({!getStringOne}); etc? – Jesse Milburn Feb 24 at 14:13
1  
You might also be interested in Javascript Remoting. developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/… – feddus Feb 24 at 14:15
up vote 13 down vote accepted

@Poet's answer handles most of the cases except where the StringOne contains Single quotes in it.

If there is a single quote like StringOne='Lewi\'s' it will break your Javascript execution, because it will render to

     var myArray = ['Lewi's']

Better approach is:

 var myArray = ['{!JSENCODE(StringOne)}', '{!JSENCODE(StringTwo)}'];

Suggestion:

If the object you want to render becomes bigger and complicated, I would recommend Serializing and Deserializing it as below:

    public string getMyArrayContent() {
         List<String> myArray = new List<String>();
         myArray.add('StringOne');
         .
         .
         myArray.Add('StringN');
         return JSON.serialize(myArray);
    }

In Javascript:

   var myArray = JSON.parse('{!JSENCODE(myArrayContent)}');
  • The JSENCODE function encodes text strings and merge field values for use in JavaScript by inserting escape characters, such as a backslash (\), before unsafe JavaScript characters, such as the apostrophe (').

  • {!JSENCODE(text)} and replace text with the merge field or text string that contains the unsafe JavaScript characters. So it not just prevents you from broken JS but also prevents XSS vulnerability if the string contains unsafe JavaScript code.

  • JSON.parse(str) is a Javascript method which takes a String and converts it into Javascript object (Array here)

share|improve this answer
2  
Good point! I always forget about those little buggers! – Poet Feb 24 at 14:28
1  
Excellent answer! Thank you very much. – Daft Feb 24 at 14:41
    
An explanation of why the JSENCODE/parse can make sense would be helpful here. – Keith C Feb 24 at 14:43
    
Poet thank you, I have faced this issue on Production org, so I became cautious now :) Daft glad that it helped you @KeithC Updated the explanation, please let me know if that would help people – karthikselva Feb 24 at 14:51
1  
I was really looking for why what you have is better than var myArray = {!myArrayContent};... – Keith C Feb 24 at 15:24

You should be able to simply do the following:

var myArray = ['{!StringOne}', '{!StringTwo}'];

Or

var myArray = [];

myArray.push({!StringOne});
myArray.push({!StringTwo});

Effectively because your two methods are just returning a String, you can just render them into the JS on your page and it'll work the same.

share|improve this answer
1  
Thanks for the answer, much appreciated! – Daft Feb 24 at 14:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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