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
SFTechySFTechy 

Method does not exist or incorrect signature: SalesforceBaseURL.GetSFBaseURL()

Hi, I'm fairly new to APEX.  I wrote a class which needs to get the Salesforce base URL of my org instance, so I can append it with a record id. I keep getting the following error message when I execute an anonymous block.  

Compile error at line 1 column 1
Method does not exist or incorrect signature: SalesforceBaseURL.GetSFBaseURL()

I'm running this in my sandbox.  Whether it be Eclipse or the Developer Console, I get the same error message.  Can someone please help?  What am I doing wrong?  Thank you so much!

Anonymous block syntax:
SalesforceBaseURL.GetSFBaseURL();

public class SalesforceBaseURL {   
    //public String SFBaseURL;    
    
    public static String GetSFBaseURL(){
        // Get the dynamic Salesforce base URL
        //SFBaseURL = Url.getSalesforceBaseUrl().toExternalForm();
        //SFBaseURL = "TESTING";
        return 'TESTING';
    }
}
pconpcon
In your anonymous block you need to have the class URL before.  So you need to run
 
URL.getSalesforceBaseUrl().toExternalForm();

More info: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_url.htm
bob_buzzardbob_buzzard
While its not well documented, I don't believe you can use classes in this way when executing anonymous code.  The returned information varies depending on which tool I use, but the Force.com IDE gives the best that I've found.

When I try your code from the IDE, I get the following error:

---

Compile error at line 6 column 26

Only top-level class methods can be declared static

---

Which suggests to me that anonymous apex actually creates some kind of class to execute your code. 

This is borne out when I try to change the static method to an instance one and use that:
 
public class SalesforceBaseURL {   
    //public String SFBaseURL;    
    
    public String GetSFBaseURL(){
        // Get the dynamic Salesforce base URL
        //SFBaseURL = Url.getSalesforceBaseUrl().toExternalForm();
        //SFBaseURL = "TESTING";
        return 'TESTING';
    }
}

SalesforceBaseURL sfbu=new SalesforceBaseURL();
sfbu.GetSFBaseURL();

as I then get the error:

--- 
apex.bytecodeinterpreter.InterpreterRuntimeException: Unable to load class: KAB_TUTORIAL/anon$SalesforceBaseURL Error Id: 567088251-343431 (965422512)
---

so I can define the class, but it looks like there's a top level class named 'anon' that my (your!) code is being wrapped in. This doesn't appear to be a real class though, as I can't just use the naming 'anon.SalesforceBaseURL'. 

The only way I've been able to get this to work is to save the class to Salesforce first and then execute methods on it via an anonymous block.
 
SFTechySFTechy
Thank you @pcon and @bob_buzzard for your help!  I updated the line that references the URL class to all upper case.  I tried instantiating an instance of the class without the static method and that worked.  I also tried using a static method without the need to instantiate tthe class and that worked now too.  I used System.Debug(SalesforceBaseURL.GetSFBaseURL()); and that executed successfully. 
 
pconpcon
I've seen this also happen if you have a variable named url in your code.  Apex gets a little confused and tries to call the variable instead of the class name.  So try to avoid using the variable name url in your code if possible.