1

I'm trying to pass a simple string to a vf page, but can't seem to. What am I doing wrong?

APEX

    public class ControllerJSGeo{
    public String pId;

    public String testString {get;set;}

    public ControllerJSGeo(ApexPages.StandardController stdController){
        pId = ApexPages.CurrentPage().getParameters().get('id');

    }

     public String testString(){
       string ts='Hello!';
      return ts;
    }

}

VF PAGE

<apex:page standardController="Property__c" extensions="ControllerJSGeo">

<head>

<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
<script type="text/javascript"> 

$(document).ready(function() {

  var myOptions = {
    zoom: 18,
    mapTypeId: google.maps.MapTypeId.HYBRID,
    mapTypeControl: true,
    scrollwheel: false
  }

  var map;
  var marker;

  var geocoder = new google.maps.Geocoder();
  var address = "{!Property__c.Property_Address__c}, " + "{!Property__c.City__c}, " + "{!Property__c.State__c} " + "{!Property__c.Zip_Postal_Code__c}}";



  var infowindow = new google.maps.InfoWindow({
    content: "<b>{!Property__c.Name}</b><br>{!Property__c.Property_Address__c}<br>{!Property__c.City__c}, {!Property__c.State__c} {!Property__c.Zip_Postal_Code__c}"
  });

  geocoder.geocode( { address: address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK && results.length) {
      if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {

        //create map
        map = new google.maps.Map(document.getElementById("map"), myOptions);
        map.setTilt(45);


        //center map
        map.setCenter(results[0].geometry.location);

        //create marker
        marker = new google.maps.Marker({
            position: results[0].geometry.location,
            map: map,
            title: "{!Property__c.Name}"
        });

        //add listeners
        google.maps.event.addListener(marker, 'click', function() {
          infowindow.open(map,marker);
        });
        google.maps.event.addListener(infowindow, 'closeclick', function() {
          map.setCenter(marker.getPosition()); 
        });

      }




    } else {
      $('#map').css({'height' : '15px'});
      $('#map').html("Oops! {!Property__c.Name}'s billing address could not be found, please make sure the address is correct.");
      resizeIframe();
    }
  });

  function resizeIframe() {
    var me = window.name;
    if (me) {
      var iframes = parent.document.getElementsByName(me);
      if (iframes && iframes.length == 1) {
        height = document.body.offsetHeight;
        iframes[0].style.height = height + "px";
      }
    }
  }

});
</script>

<style>
#map {
  font-family: Arial;
  font-size:12px;
  line-height:normal !important;
  height:800px;
  background:transparent;
}
</style>

</head>

<body>
<div id="map"></div> 
<apex:outputPanel><apex:outputText>{!testString}</apex:outputText></apex:outputPanel>

</body>
</apex:page>
4

The apex code can be modified as below

Approach 1 - Using property as getter and setters variable

public class ControllerJSGeo{
public String pId;

public String testString {get;set;}

public ControllerJSGeo(ApexPages.StandardController stdController){
    pId = ApexPages.CurrentPage().getParameters().get('id');
    testString();
}

 private  void testString(){
   testString = 'Hello!';
}

Approach 2 - The other way of doing is using get method like below

public class ControllerJSGeo{
public String pId;

public ControllerJSGeo(ApexPages.StandardController stdController){
    pId = ApexPages.CurrentPage().getParameters().get('id');
}

 public void gettestString(){
   string ts = 'Hello!';
   return ts;
}
| improve this answer | |
  • Now that I have this working, I'm trying to pass an array of strings. But, when I do this, I get "Content cannot be displayed: Attempt to de-reference a null object" – MRG May 31 '16 at 18:07
  • I used the same testString method as above, except I changed the code inside the brackets to public void testString(){ string a='string'; testString.add(a); } – MRG May 31 '16 at 18:09
  • Any idea why I'm getting that error? – MRG May 31 '16 at 18:10
  • Please put together a different question – Mohith Shrivastava May 31 '16 at 18:11
  • 1
    The list needs to be instantiated – Mohith Shrivastava May 31 '16 at 18:12
2

You can set the string in the Constructor as so.

public class ControllerJSGeo {

    public String pId;
    public String testString {get;set;}

    public ControllerJSGeo(ApexPages.StandardController stdController){
        pId = ApexPages.CurrentPage().getParameters().get('id');
        testString = 'Hello';
    }
}

Then get the value as so in the Visualforce page. Also, see below you can get the page id with the current page merge field. The Id needs to be in the url obviously. https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_variables_global_currentpage.htm

 $(document).ready(function() {
    /*
     * If you only need the Id from the current page you can get it directly
     *     by using the the current page merge field
     */
    var pId = '{!$CurrentPage.Parameters.Id}';
    console.log(pId);
    var testString = '{!testString}';
    console.log(testString);
});   

If you need variable after the page loads, you can use RemoteAction in your Apex class. I set the remote action to global so the glass has to be global as well (see here for more about global/public remote actions). The test string also needs to be static so it's visible.

Class:

global class ControllerJSGeo {

    public String pId;
    public static String testString {get;set;}

    public ControllerJSGeo(ApexPages.StandardController stdController){
        pId = ApexPages.CurrentPage().getParameters().get('id');
        testString = 'Hello';
    }

    @RemoteAction
    global static string setTestString(String myString){
        testString = myString;
        System.debug(testString);
        return testString;
    }
}

Page:

<apex:page standardController="Property__c" extensions="ControllerJSGeo">

<button id="test-button">
    Click Me!
</button>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
<script>    
$(document).ready(function() {
    var pId = '{!$CurrentPage.Parameters.Id}';
    console.log(pId);
    var testString = '{!testString}';
    console.log(testString);
    $("#test-button").click(function (){
        getTestString();
    });
});    
function getTestString() {
    var myString = 'Hello Again.';
    Visualforce.remoting.Manager.invokeAction('ControllerJSGeo.setTestString',
        myString,
        function(result, event) {
            if (event.status) {
                // Result is testString which is now set in the controller
                console.log(result);
                alert('Test String: ' + result);
            } else {
                console.log(event.message);
            }
        }, {
        escape: true
    });                                      
}
</script>

| improve this answer | |

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.