+ 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.
EM3R50NEM3R50N
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
EM3R50NEM3R50N
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)
EM3R50NEM3R50N
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
 
EM3R50NEM3R50N
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

EM3R50NEM3R50N
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.
EM3R50NEM3R50N
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.

EM3R50NEM3R50N
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.