Sie sind auf Seite 1von 8

SALESFORCE APIS WITH EXAMPLES..

May 25, 2014 by Srieniwaas in Others 3 Comments


Salesforce APIs with Real Time Example

The different Types of Salesforce APIs are mentioned below and you can also get the same info from
Salesforce Help material. My intention is to help newbies with Real Time Example which I
explained Below..

SOAP API
Use SOAP API to create, retrieve, update or delete records, such as accounts, leads, and custom objects.
With more than 20 different calls, SOAP API also allows you to maintain passwords, perform searches,
and much more. Use SOAP API in any language that supports Web services.

When to Use Soap API

The Salesforce prebuilt applications provide powerful CRM functionality. In


addition, Salesforce provides the ability to customize the prebuilt applications to fit your organization.
However, your organization may have complex business processes that are unsupported by the existing
functionality. When this is the case, the Force.com platform includes a number of ways for advanced
administrators and developers to implement custom functionality. These include the SOAP API, Apex,
and Visualforce.

REST API
REST API provides a powerful, convenient, and simple REST-based Web services interface for
interacting with Salesforce. Its advantages include ease of integration and development, and its an
excellent choice of technology for use with mobile applications and Web projects. However, if you have a
large number of records to process, you may wish to use Bulk API, which is based on REST
principles and optimized for large sets of data.

Bulk API

Bulk API is based on REST principles and is optimized for loading or deleting large sets of data. You can
use it to query, insert, update, upsert, or delete a large number of records asynchronously by submitting
batches which are processed in the background by Salesforce.

SOAP API, in contrast, is optimized for real-time client applications that update small numbers of
records at a time. Although SOAP API can also be used for processing large numbers of records, when
the data sets contain hundreds of thousands of records, it becomes less practical. Bulk API is designed to
make it simple to process data from a few thousand to millions of records.

Metadata API
Use Metadata API to retrieve, deploy, create, update, or delete customizations for your organization.
The most common use is to migrate changes from a sandbox or testing organization to your production
environment. Metadata API is intended for managing customizations and for building tools that can
manage the metadata model, not the data itself.

The easiest way to access the functionality in Metadata API is to use


the Force.com IDE or Force.comMigration Tool. These tools are built on top of Metadata API and use
the standard Eclipse and Ant tools respectively to simplify the task of working with Metadata API. Built
on the Eclipse platform, the Force.comIDE provides a comfortable environment for programmers
familiar with integrated development environments, allowing you to code, compile, test, and deploy all
from within the IDE itself. The Force.comMigration Tool is ideal if you want to use a script or a
command-line utility for moving metadata between a local directory and a Salesforce organization.

Apex
Use Apex if you want to:

Create Web services.

Create email services.

Perform complex validation over multiple objects.

Create complex business processes that are not supported by workflow.

Create custom transactional logic (logic that occurs over the entire transaction, not just with a
single record or object).

Attach custom logic to another operation, such as saving a record, so that it occurs whenever
the operation is executed, regardless of whether it originates in the user interface, a Visualforce
page, or from SOAP API.

For more information, see the Force.com Apex Code Developers Guide.

Visualforce
Visualforce

Build wizards and other multistep processes.

Create your own custom flow control through an application.

Define navigation patterns and data-specific rules for optimal, efficient application interaction.

REST API example in Salesforce


Representational State Transfer (REST) is a style of software architecture for distributed systems such
as the World Wide Web. REST has emerged as a predominant web service design model.

REST-style architectures consist of clients and servers. Clients initiate requests to servers; servers
process requests and return appropriate responses. Requests and responses are built around the
transfer of representations of resources.

The Force.com REST API provides you with a powerful, convenient, and simple Web services API for
interacting with Force.com. Its advantages include ease of integration and development, and it is an
excellent choice of technology for use with mobile applications and Web 2.0 projects. However, if you
have large numbers of records to process, you may wish to use Bulk API, which is based on REST
principles and optimized for large sets of data.

Example: Create as Apex class with Below code(EMP Object should be created in
beginning).

@RestResource(urlMapping=/EMPLOYEE__c/*)
global with sharing class sampleRest
{

@HttpDelete
global static void doDelete()
{
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String memberId = req.requestURI.substring(req.requestURI.lastIndexOf(/)+1);
EMPLOYEE __c emp = [SELECT Id FROM EMPLOYEE__C WHERE Id = :EMPId];
delete EMP;
}
@HttpGet
global static EMPLOYEE __c doGet()
{
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String memberId = req.requestURI.substring(req.requestURI.lastIndexOf(/)+1);
EMPLOYEE __c result = [SELECT Id, Name FROM EMPLOYEE __c WHERE Id =
:EMPId];
return result;
}
@HttpPost
global static String doPost(String name)
{
EMPLOYEE __c m = new EMPLOYEE __c();
e.Name = name;
insert e;
return e.Id;
}

SOAP API Example:

How to Consume External Web


Service in Salesforce APex
One of the feature we have in Salesforce is that we can easily consume External Web Services. In this
article, we will learn step by step demo of consuming Web Service in Apex. There are many public
websites available to consume Web Service and one of them, I am using in this article
is http://www.webservicex.net , from this location we are using Stock Quote Webservice. To consume
this, we need WSDL.

The Web Services Description Language (WSDL) is an XML-based interface description language that
is used for describing the functionality offered by a Web Service.

WSDL have to be downloaded from this location. We need this WSDL in later part of this tutorial.

Once, WSDL is downloaded and saved on local drive. We have to go to Salesforce and navigate
to Setup | Develop | Apex Classes. On right hand side, you will find button named as Generate
from WSDL. This button will generate equivalent Apex class to support Webservice call. In some
Programming languages, these classes are known as Proxy classes or Stubs.

When we click on Generate from WSDL button, it will prompt for WSDL File. Select WSDL
file downloaded previously and click on Parse WSDL button. On next page you will get this
error : Failed to parse wsdl: Found more than one wsdl:portType. WSDL with multiple
portType not supported.

In some cases you may also get error like : Failed to parse wsdl: Found more than one
wsdl:binding. WSDL with multiple binding not supported.

Intentionally, I have used this WSDL to explain that currently Salesforce supports only single portType
and binding.

PortType : defines a web service, the operations that can be performed, and the messages that are
involved.
Binding : WSDL bindings defines the message format and protocol details for a web service.

Reason, we are getting an error because wsdltoApex doesnt support multiple PortType, Binding,
SOAP 1.2 and Schema imports. You can read more here from Salesforce documentation.

How to resolve multiple portType and Binding error in Apex while generating stubs ?

We have to modify downloaded WSDL to make sure it only contains Single Binding and single
PortType. Before modifying I would suggest to read about WSDL elements. I have uploaded both
versions of WSDL (Please change attached file extensions from txt to xml). First one is Actual WSDL and
second I have modified to remove errors. You can compare both and check how I removed multiple
PortType and Binding elements from WSDL.

1. Stockquote Actual WSDL File (Change extension from txt to xml)

2. Stockquote Updated WSDL File (Change extension from txt to xml)


Once, you modify your wsdl, try to generate Apex class again and this time you will be able to generate
class without any error.

Generating Apex from WSDL in Salesforce

So, here we have successfully generated Stub classes for Webservice.

How to use generated web-service Apex stub ?

Before trying to use webservice, we have to inform Salesforce that our code will try to get some Data
from External source (rather interact with external system). And therefore Remote Site Setting comes
into picture.

Navigate to Setup | Security Control | Remote Site Settings.

Create new Site setting for URL http://www.webservicex.net. If we skip this step, we will endup with
error like Unauthorized Endpoint.

We can use generated Apex class in Visualforce or some other location. To keep this tutorial simple, I am
using Developer Console. Open Developer Console and press Ctrl+E.

wwwWebservicexNet.StockQuoteSoap proxyClass =
newwwwWebservicexNet.StockQuoteSoap();

String retVal = proxyClass.GetQuote(CTSH);

System.debug(retVal);

In above code CTSH is Stock symbol for Cognizant Technology Solution. You can use any other valid
Stock Symbol. Output will be visible in Console Log. We can use this output as per our need

How you will find that which class to instantiate?

As shown in above code, I have created Object for wwwWebservicexNet.StockQuoteSoap. I was able
to identify that which class to instantiate? I was able to identify by port type element name from WSDL.

How to increase Time out in Webservice ?

You may get Timeout exception while calling webservice. Default time is 10sec (At time of writing this
tutorial, may change in future release). we can use timeout_x property to increase time to wait for
response from web service. So above code can be re-written as

wwwWebservicexNet.StockQuoteSoap proxyClass =
newwwwWebservicexNet.StockQuoteSoap();

proxyClass.timeout_x = 20000 ; // timeout in milliseconds

String retVal = proxyClass.GetQuote(CTSH);

System.debug(retVal);

In above code CTSH is Stock symbol for Cognizant Technology Solution. You can use any other valid
Stock Symbol. Output will be visible in Console Log. We can use this output as per our need

How to increase Time out in Webservice ?

You may get Timeout exception while calling webservice. Default time is 10sec (At time of writing this
tutorial, may change in future release). we can use timeout_x property to increase time to wait for
response from web service. So above code can be re-written as

wwwWebservicexNet.StockQuoteSoap proxyClass =
newwwwWebservicexNet.StockQuoteSoap();
proxyClass.timeout_x = 20000 ; // timeout in milliseconds
String retVal = proxyClass.GetQuote(CTSH);
System.debug(retVal);

Limitations Of Apex WSDL:

1. It does not support multiple port bindings.

2. Inheritance is not supported in wsdl to APEX conversion.

3. Complex Object types such as Enumeration are not supported.

4. WSDL Import functionality is not supported.


I hope this tutorial will be helpful for newbies. Please post your comment and feedback about this
tutorial. I will be delighted to answer your query about this article.

Be Sociable Like it or Share it.

Das könnte Ihnen auch gefallen