Activity Forums Salesforce® Discussions How can you expose an Apex class as a REST WebService in Salesforce?

  • Saurabh

    Member
    April 18, 2017 at 1:55 pm

    Hi Manpreet

    you can expose your Apex class and methods so that external applications can access your code and your application through the REST architecture. This is done by defining your Apex class with the @RestResource annotation to expose it as a REST resource. You can then use global classes and a WebService callback method.

    Invoking a custom Apex REST Web service method always uses system context. Consequently, the current user’s credentials are not used, and any user who has access to these methods can use their full power, regardless of permissions, field-level security, or sharing rules.
    Developers who expose methods using the Apex REST annotations should therefore take care that they are not inadvertently exposing any sensitive data

    Look at the below piece of code for instance:-

    global class AccountPlan {
    webservice String area;
    webservice String region;
    //Define an object in apex that is exposed in apex web service
    global class Plan {
    webservice String name;
    webservice Integer planNumber;
    webservice Date planningPeriod;
    webservice Id planId;
    }
    webservice static Plan createAccountPlan(Plan vPlan) {
    //A plan maps to the Account object in salesforce.com.
    //So need to map the Plan class object to Account standard object
    Account acct = new Account();
    acct.Name = vPlan.name;
    acct.AccountNumber = String.valueOf(vPlan.planNumber);
    insert acct;
    vPlan.planId=acct.Id;
    return vPlan;
    } }

    Hope it may help

  • Suraj

    Member
    April 18, 2017 at 1:58 pm

    Hi Manpreet,

    Apex class and methods can be exposed for external applications access and your application through the REST architecture. Use @RestResource annotation with Apex class to expose it as a REST resource.

     

    • This reply was modified 7 years, 8 months ago by  Suraj.
  • Parul

    Member
    September 22, 2018 at 1:19 pm

    HI

    Apex REST Annotations
    Six new annotations have been added that enable you to expose an Apex class as a RESTful Web service.@RestResource(urlMapping=’/yourUrl’)
    @HttpDelete
    @HttpGet
    @HttpPatch
    @HttpPost
    @HttpPut

     

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos