0
How can you expose an Apex class as a REST WebService in Salesforce?

Open 1 Answers 58 Views Technology
How can you expose an Apex class as a REST WebService in Salesforce?

1 Answer

0

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:-

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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;

 } }

I. Programmatic Features – Salesforce Interview Questions

by (280k points)
...