Activity › Forums › Salesforce® Discussions › How can I expose an Apex class as a REST WebService in Salesforce?
Tagged: Apex Class, Field Level Security, Rest API, REST WebService, Salesforce Apex Class, Salesforce Sharing Rules, WebService Callback
-
How can I expose an Apex class as a REST WebService in Salesforce?
Posted by Laveena on August 20, 2019 at 5:10 AMHow can I expose an Apex class as a REST WebService in Salesforce?
Nikita replied 6 years, 8 months ago 2 Members · 1 Reply -
1 Reply
-
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 dataLook 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.