Newer Version Available
Considerations for Using the webservice Keyword
When using the webservice
keyword, keep the following considerations in mind:
- Use the webservice keyword to define top-level methods and outer class methods. You can’t use the webservice keyword to define a class or an inner class method.
- You cannot use the webservice keyword to define an interface, or to define an interface's methods and variables.
- System-defined enums cannot be used in Web service methods.
- You cannot use the webservice keyword in a trigger.
- All classes that contain methods defined with the webservice keyword must be declared as global. If a method or inner class is declared as global, the outer, top-level class must also be defined as global.
- Methods defined with the webservice keyword are inherently global. Any Apex code that has access to the class can use these methods. You can consider the webservice keyword as a type of access modifier that enables more access than global.
- Define any method that uses the webservice keyword as static.
- You cannot deprecate webservice methods or variables in managed package code.
- Because there are no SOAP analogs for certain Apex elements, methods defined with the
webservice keyword cannot take the following
elements as parameters. While these elements can be used within the method, they also
cannot be marked as return values.
- Maps
- Sets
- Pattern objects
- Matcher objects
- Exception objects
- Use the webservice keyword with any member variables that you want to expose as part of a Web service. Do not mark these member variables as static.
Considerations for calling Apex SOAP Web service methods:
- Salesforce denies access to Web service and executeanonymous requests from an AppExchange package that has Restricted access.
- Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
- If a login call is made from the API for a user with an expired or temporary password, subsequent API calls to custom Apex SOAP Web service methods aren't supported and result in the INVALID_OPERATION_WITH_EXPIRED_PASSWORD error. Reset the user's password and make a call with an unexpired password to be able to call Apex Web service methods.
The following example shows a class with Web service member variables and a Web service
method:
1global class SpecialAccounts {
2
3 global class AccountInfo {
4 webservice String AcctName;
5 webservice Integer AcctNumber;
6 }
7
8 webservice static Account createAccount(AccountInfo info) {
9 Account acct = new Account();
10 acct.Name = info.AcctName;
11 acct.AccountNumber = String.valueOf(info.AcctNumber);
12 insert acct;
13 return acct;
14 }
15
16 webservice static Id [] createAccounts(Account parent,
17 Account child, Account grandChild) {
18
19 insert parent;
20 child.parentId = parent.Id;
21 insert child;
22 grandChild.parentId = child.Id;
23 insert grandChild;
24
25 Id [] results = new Id[3];
26 results[0] = parent.Id;
27 results[1] = child.Id;
28 results[2] = grandChild.Id;
29 return results;
30 }
31}1// Test class for the previous class.
2@isTest
3private class SpecialAccountsTest {
4 testMethod static void testAccountCreate() {
5 SpecialAccounts.AccountInfo info = new SpecialAccounts.AccountInfo();
6 info.AcctName = 'Manoj Cheenath';
7 info.AcctNumber = 12345;
8 Account acct = SpecialAccounts.createAccount(info);
9 System.assert(acct != null);
10 }
11}You can invoke this Web service using AJAX. For more information, see Apex in AJAX.