function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JustinMccJustinMcc 

Javascript problem with callback after appendToOnloadQueue

Hello,

 

My purpose is to use the Salesforce javascript functionality to put the record count for the related lists in any location I want on the page through js.

 

I am using sfdcPage.appendToOnloadQueue to have my javascript function called when the page is loaded.  I have created a javascript object with various methods defined using prototype, and attach one of the methods from this object to get called when the page has loaded.

 

I attach as so sfdcPage.appendToOnloadQueue(this.functionName);

 

The instance properties of the class contain which related lists to process.

 

My problem is that when I receive my call to the function I scheduled with appendToOnloadQueue my object has no instantiated variables.  Why is this?

 

If I use the DOM browser I can see my class with the correct variables hanging off of window, so why does this not work?

 

I have tried removing var from in front of my variable declaration; tried adding window. in front nothing works.  I am obviously declaring it outside a function.

 

I am only a beginner at javascript so can someone help me out?

 

I can see how to get round this but for my own info would like to understand the reason behind the issue.

 

Thanks,

 

Justin

AvromAvrom

1. Can you post where and how you instantiate your object?

2. Is it definitely that your object has no defined variables? Is it possible that your object variable itself is undefined?

3. You say the object is hanging off of window? That's a bit odd. #1 would help find out why. Does it work if you refer to your object as window.yourObjectName?

 

stephanstephan

Try this instead:

 

sfdcPage.appendToOnloadQueue(function() { this.functionName.call(this) });

 

...stephan

JustinMccJustinMcc

The variable was instantiated with some inline javascript outside of any function as so:

 

var jmutil = new JMUtilRelatedList();

 

I thought that all global variables "hang off" window thats why I mentioned that it existed when viewed in the DOM browser.

 

After further tests I have noticed the following.

 

When I was appending it to the onPageLoad queue I was doing this inside the object as so:

 

JMUtilRelatedList.prototype.setAfterPageLoad =
    function()
    {
        if (!window.sfdcPage)
        {
            return;
        }
        sfdcPage.appendToOnloadQueue(this.getAndSetCountAll);
    }

 

This does not work as per my original issue, but if I append the function outside of the object itself, like:

 

var jmutil = new JMUtilRelatedList();

// add some related lists

sfdcPage.appendToOnloadQueue(jmutil.getAndSetCountAll);

 

then it works as expected.