0

Im trying to launch a flow from a button using javascript. In my code so far I have:

{!REQUIRESCRIPT("/soap/ajax/34.0/connection.js")}

String base = URL.getSalesforceBaseUrl();
alert(base);

However when I click the button I get:

Unexpected identifier

instead of the URL. What is it I am missing?

2

You can't call Apex Code methods like that; you're in a JavaScript context. You'll probably want to start off with something like this:

{!RequireScript('/soap/ajax/34.0/connection.js')}
{!RequireScript('/soap/ajax/34.0/apex.js')}

var baseUrl = sforce.apex.execute('Utils','getSalesforceBaseUrl', {});
alert(baseUrl);

You'll need a corresponding class:

global class Utils {
    webservice static string getSalesforceBaseUrl() {
        return URL.getSalesforceBaseUrl();
    }
}

You might also have different options available, depending on your exact context. This is only one possible option. For example, you could write a Visualforce page with a trivial controller:

<apex:page contentType="text/plain" controller="Utils" 
           showHeader="false" sidebar="false" applyHtmlTag="false"
           standardStylesheets="false" showChat="false">{!salesforceBaseUrl}</apex:page>

With the appropriate method in Utils:

    public string getSalesforceBaseUrl() {
        return URL.getSalesforceBaseUrl();
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.