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
Przemysław TamońPrzemysław Tamoń 

Getting '$A is not defined' error while doing VF Trailhead

Hello,

I'm doing a 'Visualforce Basics' module and I'm stuck at 'Use Standard Controllers' Unit (link here).
I created a page with the following code:
 
<apex:page>
    <apex:pageBlock title="Account Summary">
        <apex:pageBlockSection>

        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Then I opened a page via the "Preview" button in a Developer Console and opened a JavaScript console in Chrome where I typed:

$A.get("e.force:navigateToURL").setParams({"url": "/apex/AccSum"}).fire();
And I got the following error:
Uncaught ReferenceError: $A is not defined at <anonymous>:1:1
 

Both the snippets are copied from the Unit's sections, I didn't change anything except the page's name - 'AccSum'. I tried all of the above in a Firefox which also did not work.

Does anyone know what's going on?

Best Answer chosen by Przemysław Tamoń
ReidCReidC
Interestingly, I was able to reproduce the "$A is not defined" error when I open the JavaScript console from the Salesforce Developer Console. As I re-read the instructions, I would say that they aren't clear that you need to return to the Lightning Experience and then open your JavaScript console, instead of doing it from the Salesforce Developer Console.  I'll open a bug against the module.

All Answers

ReidCReidC
Are you in the Lightning Experience or are you in Classic?  $A is specific to Lightning.
Przemysław TamońPrzemysław Tamoń
@ReidC
It's said there "To preview your page in the context of Lightning Experience, open your browser’s JavaScript console and enter the following code. Don’t forget to replace pageName with your page’s name:", so I thought it would be done by the SF to show me the page in the Lightning Experience. 
Anyway, when I normally visit my instance, it's Lightning, not Classic, so I assume I'm in the Lightning Experience.
Christopher D. EmersonChristopher D. Emerson
Same here - $A throwing an undefined error for me - and I'm already in Lightning mode. Seems like X-Frame errors/issue common with SF/Lightning stuff? https://developer.salesforce.com/forums/?id=9060G000000MPPtQAO
 
HelloWorld?core.apexpages.request.devconsole=1:1 Invalid 'X-Frame-Options' header encountered when loading 'https://mysfdevdomain.my.salesforce.com/apexpages/devmode/devConsoleViewStateMetadataReceiver.apexp?sfdcIFrameOrigin=https%3A%2F%2Fmysfdevdomain--c.na40.visual.force.com': 'ALLOW-FROM https://mysfdevdomain--c.na40.visual.force.com' is not a recognized directive. The header will be ignored.
keyboard_handler.js:6 

Reload All Tabs keyboard handler loaded in this tab. Visit this extensions option, to disable.

$A.get("e.force:navigateToURL").setParams({"url":"/apex/HelloWorld"}).fire();

VM5279:1 Uncaught ReferenceError: $A is not defined
    at <anonymous>:1:1

 
ReidCReidC
Interesting!  I tried it this morning on a standard playground.
Screenshot below.  This is Chrome 62 on a Mac, nothing special.
What browser are you in?

User-added image
Christopher D. EmersonChristopher D. Emerson
Chrome | Version 61.0.3163.100 (Official Build) (64-bit)
User-added image
Przemysław TamońPrzemysław Tamoń
I did it on a Developer Edition and as said before -> Chrome and Firefox. On Windows.
ReidCReidC
So interesting!
Here's what I did:
1) Used the dev console to create the page.
2) in my browser, went back to the main screen where I could see the standard salesforce ui. (NOT the dev console)
3) Used Chrome's menu to go to Developer tools, Javascript console
4) Entered the $A text
Is that what you did?
ReidCReidC
(And by Stadnard Salesforce UI in #2 it's the Lightning Experience which is standard in a normal Trailhead Playground)
Christopher D. EmersonChristopher D. Emerson
Tried latest Firefox just now - same/similar problem. Firefox 57.0 (64-bit)
User-added image

I'm starting to wonder if it's tied to this "clickjack" settings - but these appear to be things only Salesforce themselves can change? And I'm now stuck in an infinite loop of sorts trying to get into a login that actually lets me contact SF support... fun.

User-added image
 
Christopher D. EmersonChristopher D. Emerson
ReidC here's a couple additional $A screens.

1) Called in Dev Console from my SF sandbox Setup (in Lightning mode). $A is NOT undefined here.
User-added image

2) Called from same domain as above but from my custom VisualForce Page: $A IS undefined here. 
User-added image
 
Przemysław TamońPrzemysław Tamoń

1) Created a page with the Developer Console
2) In DE Setup page: Custom Code -> Visualforce Pages -> Clicked my page's name -> Clicked "Preview"
3) Opened Developer Tools (Ctrl + Shift + I) in Chrome and clicked a Console tab
4) Typed $A

User-added image

Christopher D. EmersonChristopher D. Emerson
Przemysław I don't know if you cleared your Dev Console log but looks like you're in the same boat as me in that $A is undefined - though your initial error seems different (SSL cert issue of some kind)? 

Either way - very frustrating! We just want $A to work! =/ 
Przemysław TamońPrzemysław Tamoń
emerson

Yup, it does but I think it actually does not matter in this particular case.

ReidC

I checked "$A" on my DE instance's main page and it works - I don't get any error.
Christopher D. EmersonChristopher D. Emerson
Ok I got past the Trailhead course I was stuck on for this (https://trailhead.salesforce.com/trails/force_com_dev_beginner/modules/visualforce_fundamentals/units/visualforce_static_resources) because there were two issues at play:

1. The jQuery Mobile zip I downloaded had a different file structure than the lesson did so my CSS and image paths weren't loading right until I discovered this and fixed it. So now the basic Visualfoce Page is at least loading itself and the Static Resources correctly now (I can move on to the next course).
2. Handful of Chrome Dev Console errors about MIME types, X-Frames, $A undefind etc (Remain unresolved but not blocking course progression). I still hope to get this resolved but moving on for now. 


 
ReidCReidC
Interestingly, I was able to reproduce the "$A is not defined" error when I open the JavaScript console from the Salesforce Developer Console. As I re-read the instructions, I would say that they aren't clear that you need to return to the Lightning Experience and then open your JavaScript console, instead of doing it from the Salesforce Developer Console.  I'll open a bug against the module.
This was selected as the best answer
ReidCReidC
@EM3R50N -- in the screenshot above where you are attempting to use $A with a URL https://something.my.visual.force.com/blah -- $A will not be available from that since it is not lightning.  For the screenshot with URL for https://... lighting.force.com -- $A isn't throwing an error, but it's not doing anything since you need: $A.get("e.force:navigateToURL").setParams({"url": "/apex/YourPage"}).fire();
Przemysław TamońPrzemysław Tamoń

Ok, now I fully understood how to cope with the problem. It works of course :) Thank you! 

PS If you really think it's not clear enough, there are more places in the module where it's not mentioned that a JS console should be opened while being in a Lightning Experience.

Christopher D. EmersonChristopher D. Emerson
ReidC - thanks. That makes sense about the domains not loading the $A stuff. I have a long way to go sorting out my scratch orgs from my sandboxes from my prod environments... I get lost/confused w/them often still. This is just another example of that!  Anyhow - thanks for clearing up. 
Alexis MassonAlexis Masson
Hello guys,

I got obviously the same errors than anybody there,
I'm using the Lighning Sandbox, but when I use the preview button I got this URL coming :
https://curious-moose-283525-dev-ed--c.eu12.visual.force.com/apex/HelloWorld

That's why, listening to what said @ReidC, I tried to change the visual.force.com into .lightning.force.com/
That said, I tried :
https://curious-moose-283525-dev-ed.lightning.force.com/apex/HelloWorld
instead of 
https://curious-moose-283525-dev-ed--c.eu12.visual.force.com/apex/HelloWorld
It works but redirect to the URL endding with visual.force.com ...

If anyone could help, thanks by advance ;
I've to admit I'm not really comfortable with Salesforce ...

Thanks !
Alexis MassonAlexis Masson
Ok !
Got the anwser from here (https://developer.salesforce.com/forums/?id=9060G000000BiGtQAK)

Thanks guys !!

User-added image
Anchal SwamiAnchal Swami
Thank You ReidC
I got the same error : "Uncaught ReferenceError: $A is not defined at <anonymous>:1:1"
It has been resolved now.
Mrityunjaya TiwariMrityunjaya Tiwari
The $A is recognized on your org or app page "https://yourusername.lightning.force.com/one/one.app" or developer org. It will not work on the preview page of AccountSummary or ContactSummary "Visualforce page" you created in the developer console.

So you need to follow the steps given below to resolve the issue.
1. login on salesforce using your username and password.(if not already logged in or playground is not opened) Url:(https://login.salesforce.com/), use your trailhead credentails or dev org credential.

2. Above will open your dev org or palyground. (if it is not ligthtning then switch to lightning experience.).

3. press F12 in chrome window. 

4. click on console tab.

5. put the $A.get("e.force:navigateToURL").setParams(
    {"url": "/apex/AccountSummary"}).fire();
    //(Replace AccountSummary with the page/url you want to open).
6. press return/enter.

Note: $A is not recognized on the other pages because it's resources(libraries/assemblies/jars..) are not available there.


 
Monika PardeshiMonika Pardeshi
Thanks Mrutyunjaya.
Even i was facing this issue, followed steps given by you and it worked. Many thanks

 
Yasantha ParanamannaYasantha Paranamanna
I was getting the  "$A is not defined salesforce" error. Thank you @Mrityunjaya Tiwari for helping out!
Vishavdeep Sharma 7Vishavdeep Sharma 7
Hi,

It worked for me after enabling the flag "Available for Lightning Experience, Lightning Communities, and the mobile app" on the visual force page and then following the procedure specified by Mrutyunjaya above.

User-added image
Benjamin ViertelBenjamin Viertel
I had the same problem because the statement in the trail was unclear about what to open. Also, I think "apex/pagename" needs to be replaced, not just the "pagename" part of it. But if I do this in my cunning moose environment all I get is a plain "Hello World" in the main part of the browser while the top part with the links to Contacts, Cases, Reports etc. stays. What is the point in doing this then?
Prem KrishnanPrem Krishnan
The trailhead article is a little confusing. 
The step says "The rendered page opens in a new window. Note that this page preview shows your page without Salesforce styling. To see your page in the context of Lightning Experience, return to your main browser window. Open your browser’s JavaScript console and enter the following code. Don’t forget to replace pageName with your page’s name:
$A.get("e.force:navigateToURL").setParams( {"url": "/apex/pageName"}).fire(); "

If you try to do this in the browser window where the visual force page is previewed, it will throw error $A is not defined

To fix that, go to the browser window/tab where the Saleforce home page is open (you should ofcourse login to see the Salesforce Home page). Press F12 in chrome to open the developer window. There is a tab called Console, click on it and copy paste the code there. Hit enter and you will be able to see the Visual force page in your saleforce home page/header.
Big JoeBig Joe
The year 2021 - still the same issue - only work for the Lightning Page
J BengelJ Bengel

Just a bit later in 2021, and I can't even get it to work in Lightning. (Chrome Version 91.0.4472.124 (Official Build) (64-bit)) It's not as dramatic an error, though, it simply returns "undefined".

This is where I begin my adventure -- home page of my playgrond org, with the console open.

User-added image

What you can see but not read is this:
DevTools failed to load source map: Could not parse content for https://empathetic-moose-fc8459-dev-ed.lightning.force.com/javascript/1515605724000/ui-sfdc-javascript-impl/source/RecordGVP.js.map: Unexpected end of JSON input

DevTools failed to load source map: Could not load content for https://static.lightning.force.com/auraFW/javascript/1622757889931/aura_prod.js.map: HTTP error: status code 503, net::ERR_HTTP_RESPONSE_CODE_FAILURE

DevTools failed to load source map: Could not parse content for https://empathetic-moose-fc8459-dev-ed.lightning.force.com/javascript/1595964957000/canvas/source/CanvasRendering.js.map: Unexpected end of JSON input

DevTools failed to load source map: Could not parse content for https://empathetic-moose-fc8459-dev-ed.lightning.force.com/javascript/1571087976000/ui-analytics-reporting/source/EclairNG.js.map: Unexpected end of JSON input


Which suggests to me that we're already in trouble. And that is what I suspect is causing the later problem:

User-added image
Which is described as:

User-added image
And since it's still too small to read:

$A.get("e.force:navigateToURL").setParams(
    {"url": "/apex/AccountSummary?&id=00141000004jkU0AAI"}).fire();

undefined

I assume this is the more graceful version of :
VM75:1 Uncaught ReferenceError: $A is not defined
    at eval (eval at <anonymous> (AccountSummary:1), <anonymous>:1:1)
    at <anonymous>:1:1
eval @ VM75:1
(anonymous) @ VM30:1


I had actually guessed that you had to start from somewhere in Lightning (or at least Salesforce) in order to make that work so that everythign would be properly instantiated, but it seems that the problem begins well before we even mention $A.

I've managed to use the URL manipulation trick to get around this and continue working, but if there's anything later in the Unit that can't be done without the broswer console, I'm up the creek.