New Batch#100 (10th Nov 2021) - Salesforce Admin + Dev Training (WhatsApp: +91 - 8087988044) :https://t.co/p4F3oeQagK

Showing posts with label web services. Show all posts
Showing posts with label web services. Show all posts

Tuesday, 15 January 2013

Difference between SOAP and Restful Webservice

* REST and SOAP are the two web service frameworks available on the Force.com platform. 
* The SOAP API uses authentication with username and password, and communicates using XML messages. There are different API's to work with the Enterprise, Partner, Bulk, Metadata etc. 
*The REST API uses HTTP to authenticate, it uses a token authentication and can use OATH to authenticate, it communicates using JSON messages. 
*REST API is generally lighter weight than the SOAP API, works well with mobile applications especially.

Thursday, 16 August 2012

Invoking Callouts Using Apex - 2

SOAP Services: Defining a Class from a WSDL Document
* Classes can be automatically generated from a WSDL document that is stored on a local hard drive or network.
* Creating a class by consuming a WSDL document allows developers to make callouts to the external Web service in their Apex scripts.
Note:
- Use Outbound Messaging to handle integration solutions when possible.
- Use callouts to third-party Web services only when necessary.
To generate an Apex class from a WSDL:
1. In the application, click Your Name ➤ Setup ➤ Develop ➤ Apex Classes.
2. Click Generate from WSDL.
3. Click Browse to navigate to a WSDL document on your local hard drive or network, or type in the full path. This WSDL document is the basis for the Apex class you are creating.
Note:
- The WSDL document that you specify might contain a SOAP endpoint location that references an outbound port.
- For security reasons, Salesforce restricts the outbound ports you may specify to one of the following:
- 80: This port only accepts HTTP connections.
- 443: This port only accepts HTTPS connections.
- 1024–66535 (inclusive): These ports accept HTTP or HTTPS connections.
4. Click Parse WSDL to verify the WSDL document contents.
- The application generates a default class name for each namespace in the WSDL document and reports any errors.
- Parsing will fail if the WSDL contains schema types or schema constructs that are not supported by Apex classes, or if the resulting classes exceed 1 million character limit on Apex classes.
- For example, the Salesforce SOAP API WSDL cannot be parsed.
5. Modify the class names as desired.
- While you can save more than one WSDL namespace into a single class by using the same class name for each namespace, Apex classes can be no more than 1 million characters total.
6. Click Generate Apex.
- The final page of the wizard shows which classes were successfully generated, along with any errors from other classes.
- The page also provides a link to view successfully-generated code.
* The successfully-generated Apex class includes stub and type classes for calling the third-party Web service represented by the WSDL document.
* These classes allow you to call the external Web service from Apex.
Note the following about the generated Apex:
* If a WSDL document contains an Apex reserved word, the word is appended with _x when the Apex class is generated.
* For example, limit in a WSDL document converts to limit_x in the generated Apex class.
* If an operation in the WSDL has an output message with more than one element, the generated Apex wraps the elements in an inner class.
* The Apex method that represents the WSDL operation returns the inner class instead of the individual elements.
* After you have generated a class from the WSDL, you can invoke the external service referenced by the WSDL.
How to run generated class from WSDL
BookrWs.BookWs stub = new BookrWs.BookWs();//Top level class name.Inner level class name

stub.sessionHeader = new BookrWs.sessionHeader_Element();

stub.sessionHeader.sessionID = userInfo.getSessionID();

stub.insertBook('SOAP19','VASU',1000);

Refer Here
* Salesforce API Integration Using SOAP-based Web Services

                   Previous                                                                                              Next

Invoking Callouts Using Apex

Invoking Callouts Using Apex
* An Apex callout enables you to tightly integrate your Apex with an external service by making a call to an external Web service or sending a HTTP request from an Apex script and then receiving the response.
 Tightly Couple Architecture

* Apex provides integration with Web services that utilize SOAP and WSDL, or HTTP services (RESTful services).
Note:
* Before any Apex callout can call an external site, that site must be registered in the Remote Site Settings page, or the callout fails.
* Salesforce prevents calls to unauthorized network addresses.
* Callouts enable Apex to invoke external web or HTTP services.
*Apex Web services allow an external application to invoke Apex methods through Web services.
Adding Remote Site Settings
To add a remote site setting:
1. Click Your Name ➤ Setup ➤ Security Controls ➤ Remote Site Settings.
2. Click New Remote Site.
3. Enter a descriptive term for the Remote Site Name.
4. Enter the URL for the remote site.
5. Optionally, enter a description of the site.
6. Click Save.
Types of WSDL's
1. Enterprise WSDL
    * It is a big WSDL which contains information about schema of organization.
    * Strictly Typed: For Integer if we give 3.19 like that then it will not accept and throw error.
    * When schema is modified then we have to recreate new enterprise WSDL.
2. Partner WSDL
    * Loosly Typed: For Integer if we give 3.19 like that then it will accept and not throw error.
    * When schema is modified then it will automatically updated.

                   Previous                                                                                              Next

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

Friday, 10 August 2012

Callout Integration and Apex

Question:
I have to call to an external webservice everytime a new account is created. My question is, what is the best way to do so?

I thougt about creating an after create trigger that will invoke the method in the class that was generated from the WSDL import. Would that make sense?

If so, how can I import a class into a trigger to use its methods?
Is it the same with Http request or can i make the http request directly from the trigger?

Solution:
Currently in salesforce we can not make callout from trigger directly , You need to write a future class that will invoke the callout using wsdl generated classes. So in fact this call will be asynchronous call rather than realtime callout.

For example-  you have a wsdl genarated class named "WSDlGeneratedClass" , And you will have to write a future method like -
public class future class
{
 @future(callout=true)
 public static void calloutMethod()
 {
  // write code to invoke "WSDlGeneratedClass" class methods to make callout
 }
}

Now this method you will need to call from trigger.

Yes its also with HTTP callout  (cannot make any kind of callout from trigger directly).

Saturday, 14 July 2012

Web Services

  
* Web Services can convert your applications into Web-applications.
* Web Services are published, found, and used through the Web.
* Web services are typically application programming interfaces (API) or Web APIs that are accessed via Hypertext Transfer Protocol (HTTP) and executed on a remote system hosting the requested services. 
* Web services are application (software) components that run over the internet using XML.
* Web services can be used by other applications. 
How Does it Work?
* The basic Web services platform is XML + HTTP.
*XML provides a language which can be used between different platforms and programming languages and still express complex messages and functions.
*The HTTP protocol is the most used Internet protocol.
*Web services platform elements:
SOAP (Simple Object Access Protocol)
UDDI (Universal Description, Discovery and Integration)
WSDL (Web Services Description Language)

Exposing Apex Methods as Web Services:
 * To expose your Apex methods, use WebService.
* You can expose your Apex methods so that external applications can access your
code and your application.
Note:
* Apex Web services allow an external application to invoke Apex methods through Web services.
   (Nothing but 'call in', external application use our apex methods)
* Apex callouts enable Apex to invoke external web or HTTP services.

WebService Methods:
* Apex class methods can be exposed as custom Force.comWeb services API calls.
* This allows an external application to invoke an Apex web service to perform an action in Salesforce.
*Use the webService keyword to define these methods.
global class MyWebService {
webService static Id makeContact(String lastName, Account a) {
Contact c = new Contact(lastName = 'Weissman', AccountId = a.Id);
insert c;
return c.id;
}
}
*A developer of an external application can integrate with an Apex class containing webService methods by generating a WSDL for the class.
*To generate a WSDL from an Apex class detail page:
1. In the application navigate to Your Name ➤ Setup ➤ Develop ➤ Apex Classes.
2. Click the name of a class that contains webService methods.
3. Click Generate WSDL.
Exposing Data with WebService Methods:
* Invoking a custom webService method always uses System context.
* Apex class methods that are exposed through the API with the webService keyword do not observe object permissions, field-level security, or sharing rules for any records, unless the methods are contained in a class defined using the with sharing keyword.
* Only classes defined using with sharing respect sharing rules for current user.


Labels