[SalesForce] Submit Form Page

We need to create a page in the Salesforce application, which will do the following things: page contains submit form, on submit event it should call external API. Depends on API response page should show success or error message. No data should be saved in salesforce database.
Could you please advise what mechanism should we use: pages, layouts, etc… I am very new with salesforce so I am looking for ANY help.
Thank you.

Best Answer

Here is a contrived example of a controller that makes a request to the Bing search engine:

public with sharing class DemoController {

    public String text {get; set;}
    public String html {get; set;}

    public DemoController() {
        text = 'adele hello';
    }

    public PageReference search() {

        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://www.bing.com/search?q='
                + EncodingUtil.urlEncode(text, 'UTF-8'));
        req.setMethod('GET');

        HttpResponse res = new Http().send(req);
        html = res.getBody();

        return null;
    }
}

and just dumps the resulting response HTML back into the same page:

<apex:page controller="DemoController">
    <apex:form>
        <apex:pageMessages/>
        <apex:sectionHeader title="Demo"/>
        <apex:pageBlock>
            <apex:pageBlockButtons>
                <apex:commandButton value="Search" action="{!search}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Search">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Text"/>
                    <apex:inputText value="{!text}"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Result">
                <apex:outputText escape="false" value="{!html}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

The first time you run it you will be receive an error message that explains that you need to allow access to that remote site - just follow the instructions that are presented. Thereafter the page should just work.

Unlike Bing, many external APIs do require authentication which adds complexity.

Related Topic