6

I'm trying to convert some C# code I wrote to access a third party REST service into APEX. It's failing with an error:

"Variable does not exist: String"

at the first line of the getSecureUrl method. I checked both variables I'm passing in and they're valid instances with values inside, so I'm not sure where its breaking down and the error message is just not helpful. Here's the code that's causing the error:

private string getSecureUrl(
    String method,
    List<String> parameters) {
    String requestUrl = String.format('https://blargh.com/v2/{0}', method); // error!

    return String.format(requestUrl, parameters);
}

/// Authorization Methods   ============================================================================
private void authorize(
    String userName,
    String token)  {
    String requestUrl = this.getSecureUrl('UserAuth/login?username={0}&app_token={1}', new String[] {
        userName,
        token
    });

    this.getResponse(requestUrl);
}

I'd appreciate some help because APEX is driving me out of my mind...

2 Answers 2

6

You are passing a single string method as the second parameter to the string.format method. I think it is confusing the compiler into thinking you are looking for a variable called String.

Try:

private string getSecureUrl(
    String method,
    List<String> parameters) {
    String requestUrl = String.format('https://blargh.com/v2/{0}', new List<string>{method});

    return String.format(requestUrl, parameters);
}

Apex doesn't have the C# concept of params. So you need to explicitly put the into to List<String> or String[].

4
  • That was it. Thanks for the help, it would have probably taken me several days before I realized that it should be a list. I'm finding that C# to APEX is difficult...
    – Gup3rSuR4c
    Commented May 6, 2015 at 23:33
  • Quick work! I'll leave my answer as it has a slightly different take. Commented May 6, 2015 at 23:34
  • @CaspNZ +1 for you, but I got you on the draw :) Commented May 6, 2015 at 23:35
  • You don't get to 26K for nothin' ;) Commented May 6, 2015 at 23:48
3

String.format takes a list - you need to pass one to it. Eg:

String requestUrl = String.format('https://blargh.com/v2/{0}', new String[]{method}); // error!

You must log in to answer this question.

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