0

I have a working view and controller. I've added a custom link to accounts/leads that passes the id and type in the query string to an page and here the controller should then load some data according to that ID.

My soql works fine, so thats not the issue. I think it has something to do with things not happening in the right order. I was terrible at that with web forms aswell..

Heres my code:

public MapObject clickedObject { get; private set; }

public PageReference init(){
      return LocalizationMap();
}

public PageReference LocalizationMap() {
    string currentObject = ApexPages.currentPage().getParameters().get('id');
    string currentObjectType = ApexPages.currentPage().getParameters().get('type');

    String q = 'SELECT Id, Name, Geolocation__Latitude__s, Geolocation__Longitude__s FROM ' + currentObjectType + ' WHERE id = \'' + currentObject + '\' Limit 1';

    if(currentObjectType.toLowerCase() == 'account'){
        List<Account> results = Database.query(q);
        clickedObject = new MapObject(results[0]);
    }else{
        List<Lead> results = Database.query(q);
        clickedObject = new MapObject(results[0]);
    }
    return null;
}

and then in my view I have javascript (on document ready) calling a RemoteAction for placing the marker on a map, but in the view the {!clickedObject.rLat} and {!clickedObject.rLon} are empty.

What am I doing wrong?

0

Presumably you are sure that id and type are present in the query string and have appropriate values?

One way of avoiding many execution order problems is to "lazily load" property values and so make them automatically initialise when they are referenced. For your code that would be:

public MapObject clickedObject {
    get {
        if (clickedObject == null) {
            string currentObject = ApexPages.currentPage().getParameters().get('id');
            string currentObjectType = ApexPages.currentPage().getParameters().get('type');
            String q = 'SELECT Id, Name, Geolocation__Latitude__s, Geolocation__Longitude__s FROM ' + currentObjectType + ' WHERE id = \'' + currentObject + '\' Limit 1';
            if(currentObjectType.toLowerCase() == 'account'){
                List<Account> results = Database.query(q);
                clickedObject = new MapObject(results[0]);
            }else{
                List<Lead> results = Database.query(q);
                clickedObject = new MapObject(results[0]);
            }
        }
        return clickedObject;
    }
    private set;
}

But the "quick fix" for your page might just be to add:

<apex:page ... action="{!init}">

as this is guaranteed to run before the page is rendered and so before the DOM is ready and will get clickedObject initialised.

| improve this answer | |
  • Yes they are both getting passed on. The page opens correctly and stuff, just that the result from the RemoteAction is empty. I tried your "quick fix" but this gives me an error; SObject row was retrieved via SOQL without querying the requested field: Account.Type Error is in expression '{!init}' in component <apex:page> in page localizationmap: Class.LocalizationMap.MapObject.<init>: line 47, column 1 Class.LocalizationMap.LocalizationMap: line 136, column 1 Class.LocalizationMap.init: line 234, column 1 Running between meetings, but Ill digg into it more in a min! – Cammy Jul 25 '14 at 13:21
  • @Cammy That error looks like the LocalizationMap tries to reference the Account Type field you have not queried. If that is the case you can add Type to the select statement. – Keith C Jul 25 '14 at 13:35
  • @Cammy Wrong class - its the MapObject (constructor). – Keith C Jul 25 '14 at 13:46
  • That was indeed it! Stupid soql that you have to fetch all the fields you need vs Tsql. Thanks a bunch! It worked, now Im getting hits on init. Some jquery error though with to much recursion, have to check that after next meeting :) – Cammy Jul 25 '14 at 14:03

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.