I have a separate .js
file and I want to access controller properties and functions using that file but as far as I know we must have a .vfp
page and a controller attribute is given to apex:page
, but here I don't have a .vfp
page. I just have a .js
file.
Is this possible?
-
Is it lightning or classic– Pranay JaiswalCommented Sep 20, 2018 at 7:43
Add a comment
|
1 Answer
yes, it is possible. You can use AJAX Toolkit and Apex AJAX (part of AJAX Toolkit)
e.g. login example
try{
var result = sforce.connection.login("[email protected]", "password");
log("logged in with session id " + result.sessionId);
}catch(error) {
if (error.faultcode.indexOf("INVALID_LOGIN") != -1) {
log("check your username and passwd, invalid login");
} else {
log(error);
}
}
your apex method should be exposed as webservice
and class should have global
access modifier.
global class myClass{
webservice static String getContextUserName() {
return UserInfo.getFirstName();
}
}
example of calling such apex method
var contextUser = sforce.apex.execute("myClass", "getContextUserName", {});
-
This isn't a VF controller/extension, though. Perhaps if you had easy access to a RemoteAction method, I would consider this a viable answer.– sfdcfox ♦Commented Sep 20, 2018 at 11:31