0

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?

1
  • Is it lightning or classic Commented Sep 20, 2018 at 7:43

1 Answer 1

3

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", {});
1
  • 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .