Hello Everyone, In this tutorial, I am going to describe “Webservice” keyword in Apex. If you want to expose your apex class as a SOAP web service, “webservice” keyword there for you. SOAP web service is as easy as with REST.  In this unit, I will point-out some properties of this keyword and also give you a short and simple code example that will help to understand “webservice” keyword in a better way.

After completing this unit, you’ll able to,
  • Use of “webservice” keyword in Salesforce.
So let’s get started,

Syntax:
 global class MyWebService {
    webservice static Contact getRecord(String id) {
        // Add your code
    }
 }

Some properties of "webservice" keyword
Here, I am point-out some basic properties of Webservice keyword-
1. All classes that contain methods defined with the webservice keyword must be declared as global.
2. Webservice can’t be used in a trigger.
3. Webservice keyword can’t be used to define interface.
4. In webservice method system defined enums can’t be used.
5. Webservice method must be declared static
6. Webservice methods don’t enforce object permissions, field-level security and sharing rule by default.
7. WebService method always executes in the system context.
8. A method defined with the webservice keyword can’t take the following elements as parameters and they also can’t be marked as the return value.
  • Maps
  • Sets
  • Pattern objects
  • Matcher objects
  • Exception objects
Example :
 global class MyWebService {
    webService static Id CreateAccount(String accountName) {
        Account a = new Account(Name = accountName);
        insert a;
        return a.id;
    }
 }

See also:

Conclusion:
Hope you like this tutorial, for any query or suggestions please feel free to comment.
Thank you.