0

Hello I am completely new with VF I am trying to pass some data from an Apex controller to a VisualForce. The situation is the next one I want to display in Google maps by using the Google Maps Api the addresses from three different objects, So I made this apex controller:

public class MapCECompleto_Controller {

public static PageReference getCompleteList(){
    List<Object> recordsToReturn = new List<Object>();

    List<SObject1> listOne = [SELECT Id,Direccion__c, Estado__c, Name FROM SObject1];

    if(!listOne.isEmpty()){
        for(SObject1 iItem: listOne){
            if(iItem.Estado__c == HelperClass.ESTADO_ACTIVADO){
                Map<String, Object> oneMap = new Map<String, Object>();
                oneMap.put('Name', iItem.Name);
                oneMap.put('Direccion', iItem.Direccion__c);
                oneMap.put('Objeto', 'Object 1');
                oneMap.put('Id',iItem.Id );

                recordsToReturn.add(oneMap);
            }
        }
    }

    List<SObject2> listTwo =  [SELECT Id,EstadoSolicitud__c,Direccion__c,, Name,  FROM SObject2];

    if(!listTwo.isEmpty()){
        for(SObject2 iItem: listTwo){
            if(iItem.EstadoSolicitud__c == HelperClass.ESTADO_NUEVA || iItem.EstadoSolicitud__c == HelperClass.ESTADO_EN_GESTION){
                Map<String, Object> twoMap = new Map<String, Object>();
                twoMap.put('Name',iItem.Name );
                twoMap.put('Direccion', iItem.Direccion__c);
                twoMap.put('Objeto','Object 2');
                twoMap.put('Id',iItem.Id );
                recordsToReturn.add(twoMap);
            }
        }
    }

    List<SObject3> listThree = [SELECT Id,Direccion__c, Name FROM SObject3 LIMIT 3];
    if(!listThree.isEmpty()){
        for(SObject3 iItem: listThree){
            Map<String, Object> threeMap = new Map<String, Object>();
            threeMap.put('Name', iItem.Name);
            threeMap.put('Direccion',iItem.Direccion__c);
            threeMap.put('Objeto','Object 3');
            threeMap.put('Id',iItem.Id);
            recordsToReturn.add(threeMap);
        }
    }

    List<SObject4> listFour = [SELECT Id, Direccion__c, Name FROM SObject4 LIMIT 3];

    if(!listFour.isEmpty()){
        for(SObject4 iItem :listFour){
            Map<String, Object> fourthMap = new Map<String, Object>();
            fourthMap.put('Name', iItem.Name);
            fourthMap.put('Direccion',iItem.Direccion__c);
            fourthMap.put('Objeto','Object 4');
            fourthMap.put('Id',iItem.Id);
            recordsToReturn.add(fourthMap);
        }
    }

    System.debug('recordsToReturn ------ '+ recordsToReturn);
    return recordsToReturn;
}  
} 

So once I have this data in my recordsToReturn variable I want to pass this to the visualForce page. My VF is this one:

<apex:page controller="MapCECompleto_Controller">

<apex:pageBlock title="Transactions">
    <head>
        <script type="text/javascript" src="{!googleMapsApi}"></script>
        <script type="text/javascript">
                function initialize(){
                    var resultados = {!listaDatosCompletos}
                    console.log(resultados);
                    var myOptions= {
                        zoom: 20,
                        mapTypeId: google.maps.MapTypeId.HYBRID,
                        mapTypeControl: true
                    }
                    var geocoder = new google.maps.Geocoder();
                    console.log('aqui1');
                    console.log('resultados ---- ');
                    var map = new google.maps.Map(document.getElementById('map'), myOptions);
                }
                google.maps.event.addDomListener(window, 'load', initialize);
        </script>
        <style>
            #map{
                font-family: Arial;
                font-size: 12px;
                line-height: normal !important;
                height: 500px;   
                background: transparent;
            }
        </style>
    </head>
    <body>
        <div id='map'></div>
    </body>
</apex:pageBlock>

</apex:page>

So I want that everytime the user finds this map to bring automatically this data like with the connectedCallback() method from LWC. The problem is that I have not found the way to pass the apex variable recordToReturn to the VisualForce Page. Could anybody help me? I would appreciate it. I have been hours and hours trying to solve this but I have had no success.

1 Answer 1

0

As per my understanding , there is no equivalent of connectedCallback in visualforce page but for the use case mentioned , you can use controller constructor.

This means any initialization activities(expect DMLs) can be performed in controller constructor.

Apex Code:

1.Declare a public (optionally transient) property recordsToReturn.(Looking at the description, transient property would be apt IMO )

2.For the method getCompleteList(), change the return type to List<Object>.

3.Add constructor and call getCompleteList to set the public property.

public class MapCECompleto_Controller {
   public List<Object> recordsToReturn{get;set;}

   MapCECompleto_Controller() {
      //Use class name since getCompleteList is static
      this.recordsToReturn = MapCECompleto_Controller.getCompleteList();
   }
   public static List<Object> getCompleteList(){
      recordsToReturn = new List<Object>();
      //Rest of the code remains same
    
      return recordsToReturn;
    }

Visual force page :

<apex:page controller="MapCECompleto_Controller">

<apex:pageBlock title="Transactions">
    <head>
        <script type="text/javascript" src="{!googleMapsApi}"></script>
        <script type="text/javascript">
                //Directly access the public property after parsing it
                var recordsFromApex = '{!recordsToReturn}';
                var parsedRecords = JSON.parse(recordsFromApex);
                
                function initialize(){
                    var resultados = {!listaDatosCompletos}
                    console.log(resultados);
                    var myOptions= {
                        zoom: 20,
                        mapTypeId: google.maps.MapTypeId.HYBRID,
                        mapTypeControl: true
                    }
                    var geocoder = new google.maps.Geocoder();
                    console.log('aqui1');
                    console.log('resultados ---- ');
                    var map = new google.maps.Map(document.getElementById('map'), myOptions);
                }
                google.maps.event.addDomListener(window, 'load', initialize);
        </script>
        <!-- No change below -->
</apex:pageBlock>

Note : I have not executed this code. But i think there are few compile time & runtime errors in the original code in question.

VisualForce page execution order : https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_lifecycle_example.htm

You must log in to answer this question.

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