0

I am new with VisualForce and I am trying to get a list of objects from Apex to my VisualForce file.

My Apex class is this:

public class MapCECompleto_Controller {
 List<Object> results;
public static String GOOGLE_MAPS_API = 'https://maps.googleapis.com/maps/api/js?key=';
public static String getGoogleMapsApi(){
    String developerNameIntegracion = 'Google_' + Label.SalesforceOrg; //Google_dev
    
    String authToken = IntegracionHelperClass.getAuthToken(developerNameIntegracion);
    
    String googleMapsURLWithToken = GOOGLE_MAPS_API + authToken;
    System.debug('googleMapsURLWithToken ---- '+ googleMapsURLWithToken);
    
    return googleMapsURLWithToken;
}
@RemoteAction

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);
    results = recordsToReturn;
    return null;
}  
} 

And my VF file:

    <apex:page controller="MapCECompleto_Controller" action=" {!getCompleteList}">
    
<apex:pageBlock title="Transactions">
    <head>
        <script type="text/javascript" src="{!googleMapsApi}"></script>
        <script type="text/javascript">
            $(document).ready(
                function(){
                    var myOptions= {
                        zoom: 20,
                        mapTypeId: google.maps.MapTypeId.HYBRID,
                        mapTypeControl: true
                    }
                    var map;
                    var marker;
                    var geocoder = new google.maps.Geocoder();
                    var nombre = '';
                    console.log('aqui1');
                    Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.QX_VF_MapCECompleto_Controller.getListaDatosCompletos}',function(result, event){
                        
            
                    }
                }
             )
        </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>

But the problem is that when I save the file I inmidiatly get this error message that I have not being able to solve: Return type of an Apex action method must be a PageReference. Found: core.apexpages.el.adapters.ApexListELAdapter

1 Answer 1

0

The issue is that you're trying to return the list of values that you have queried, which isn't what VF Actions are for. The simplest way to resolve this issue is to store the values that you've retrieved within a variable within the Apex Controller and return null as the page reference from your getCompleteList method.

This would mean declaring the following at the beginning of your class:

List<sObject> results;

You'll also need to remove the 'static' keyword from your getCompleteList method as this stops it being able to reference the variables declared outside of the method.

And at the end of your getCompleteList method:

results = recordsToReturn;
return null;

You can then reference the list of results in your VF Page using the annotation "{!results}".

There's a (simple) example of this in this help article under the Setter Methods section: Controller Methods | Visualforce Developer Guide

4
  • Hello I updated the code with what you mentioned, but I am getting the error Compile Error: Variable does not exist: results in the section results = recordsToReturn; The simple example you put does not initialise the results. Do you know how this could be corrected? Commented Mar 29, 2023 at 8:49
  • I've just updated the answer, you will also need to remove the 'private' key work from your method. Commented Mar 29, 2023 at 9:19
  • But I do not have any 'private' keyword in my method. Commented Mar 29, 2023 at 9:23
  • Sorry, my bad. I meant static. Commented Mar 29, 2023 at 9:30

You must log in to answer this question.

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