Considerations for Using the webservice Keyword
When using the webservice keyword, keep the following considerations in mind:
-
Use the
webservicekeyword to define top-level methods and outer class methods. You can’t use thewebservicekeyword to define a class or an inner class method. -
You cannot use the
webservicekeyword 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
webservicekeyword in a trigger. -
All classes that contain methods defined with the
webservicekeyword must be declared asglobal. If a method or inner class is declared asglobal, the outer, top-level class must also be defined asglobal. -
Methods defined with the
webservicekeyword are inherently global. Any Apex code that has access to the class can use these methods. You can consider thewebservicekeyword as a type of access modifier that enables more access thanglobal. -
Define any method that uses the
webservicekeyword asstatic. -
You cannot deprecate
webservicemethods or variables in managed package code. -
Because there are no SOAP analogs for certain Apex elements, methods defined with the
webservicekeyword 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
webservicekeyword with any member variables that you want to expose as part of a Web service. Do not mark these member variables asstatic.
Considerations for calling Apex SOAP Web service methods:
- Salesforce denies access to Web service and
executeanonymousrequests from an AppExchange package that hasRestrictedaccess. - 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:
You can invoke this Web service using AJAX. For more information, see Apex in AJAX.