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

  • Nikita

    Member
    August 20, 2019 at 6:16 am

    Hi Laveena,

    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;
        } 
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos