Search code examples
salesforceapex-codeapex

Apex only for force.com hosted apps?


Is Apex only permitted on “native” applications that are hosted on force.com?

Or is Apex also available for external applications to hit the “Open APIs” such as REST API and Bulk API?

I think part of my confusion lies in how the term “Rest API” is used in various documents. In other parts of the software world, REST is usually means an HTTP based protocol to exchange data across different domains (and with certain formats, etc ). However, I think Rest API in sales force might SOMETIMES refer to an optional means for native apps to retrieve salesforce data from within force.com. Is that correct?


Solution

  • Not sure I understand your question...

    Apex can be used "internally" in:

    • database triggers,
    • classes
      • Visualforce controllers that follow MVC pattern,
      • logic that parses incoming emails and for example makes Case or Lead records out of them,
      • asynchronous jobs that can be scheduled to recalculate some important stuff every night
      • and you can have utility classes for code reuse across these

    A "kind of internal" would be to use the "Execute Anonymous" mechanism that lets you fire one-off code snippets against environment. Useful for prototyping of new classes, data fixes etc. You can do it for example in Eclipse IDE or the Developer Console (upper right corner next to your name).


    And last but not least - "external" usage.

    Apex code can be exposed as webservice and called by PHP, .NET, Java, even JavaScript applications. It's a good choice when:

    • you want to reuse same piece of logic for example on your own Visualforce page as well as in some mobile application that would be passing couple strings around or a simple JSON object
    • beats having to reimplement the logic in every new app and maintaining that afterwards
    • imagine insertion of Account and Contact in one go - your mobile device would have to implement some transaction control and delete the Acc if the Contact failed to load. Messy. And would waste more API calls (insert acc, insert con, ooops, delete acc). With a method exposed as webservice you could accept both parameters into your Apex code, do your magic and well, if it fails - it's all in one transaction so SF will roll it back for you.

    There are 2 main methods:

    • SOAP API primarily uses global methods marked with webservice keyword. Easiest way for other applications to start calling these is to extract from SF and "consume" so-called "enterprise WSDL" file. It's a giant XML file that can be parsed in your .NET app to generate code that will help you write code you're familiar with. These generated classes will construct the XML message for you, send it, process the response (throw your own exceptions if SF has sent an error message) and so on.

    Very simple example:

    global class MyWebService {
        webService static Id makeContact(String lastName, Account a) {
            Contact c = new Contact(lastName = 'Weissman', AccountId = a.Id);
            insert c;
            return c.id;
        }
    }
    
    • REST API allows you to do similar things but you need to use correct HTTP verbs ("PUT" is best for inserts, "PATCH" for updates", "DELETE" and so on).

    You can read more about them in the REST API guide: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_rest_methods.htm|StartTopic=Content%2Fapex_rest_methods.htm|SkinName=webhelp