Thursday, 16 August 2012

Web Services - 2

Considerations for Using the WebService Keyword
* You cannot use the webService keyword when defining a class.
* However, you can use it to define top-level, outer class methods, and methods of an inner class.
* 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 because you cannot define a method 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.
* These methods can be used by any Apex script that has access to the class.
* You can consider the webService keyword as a type of access modifier that enables more access than global.
* You must 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
* You must use the webService keyword with any member variables that you want to expose as part of a Web service. You should not mark these member variables as static.
* 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.
 *The following example shows a class with Web service member variables as well as a Web service method:
global class SpecialAccounts {
global class AccountInfo {
WebService String AcctName;
WebService Integer AcctNumber;
}
WebService static Account createAccount(AccountInfo info) {
Account acct = new Account();
acct.Name = info.AcctName;
acct.AccountNumber = String.valueOf(info.AcctNumber);
insert acct;
return acct;
}
WebService static Id [] createAccounts(Account parent,
Account child, Account grandChild) {
insert parent;
child.parentId = parent.Id;
insert child;
grandChild.parentId = child.Id;
insert grandChild;
Id [] results = new Id[3];
results[0] = parent.Id;
results[1] = child.Id;
results[2] = grandChild.Id;
return results;
}
TestMethod static void testAccountCreate() {
AccountInfo info = new AccountInfo();
info.AcctName = 'Manoj Cheenath';
info.AcctNumber = 12345;
Account acct = SpecialAccounts.createAccount(info);
System.assert(acct != null);
}
}
Overloading Web Service Methods
* SOAP and WSDL do not provide good support for overloading methods.
* Consequently, Apex does not allow two methods marked with the webService keyword to have the same name.
*Web service methods that have the same name in the same class generate a compile-time error.
                   Previous                                                                                              Next

No comments:

Post a Comment

Labels