SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Downloaden Sie, um offline zu lesen
Webservices In Salesforce (Part 1)
Presenter: Surya Kanta Mekap,
Mindfire Solutions
Email: suryam@mindfiresolutions.com
SkypeId: mfsi_suryam
Date: 4 Oct 2013
Overview
1. Introduction
2. What is SOAP?
3. What is REST?
4. SOAP vs REST
5. What to Choose?
6. SOAP Webservice
7. Public SOAP Webservice
8. SOAP Callout
9. Testing Webservice Callout
10. Summary
Webservice?
Webservice is a sofware function or method of an application exposed
to the outside world allowing other application to invoke it from the
web.
What is SOAP?
The Simple Object Access Protocol(SOAP) web service is a
architecture pattern, which specifies the basic rules to be considered
while designing web service platforms. The SOAP message itself
consists of an envelope, inside of which are the SOAP headers and
body, the actual information we want to send. It is based on the
standard XML format, designed especially to transport and store
structured data. SOAP may also refer to the format of the XML that the
envelope uses.
Best for activity oriented services. An activity is more than just
insert or update or delete a record.
What is REST?
Representational State Transfer(REST) is another architectural
pattern. Unlike SOAP, RESTful applications use the GET, POST, PUT
and DELETE to perform CRUD operations. REST is resource-oriented
and uses URI (or RESTful URLs).
Roy Fielding is the Man who introduced the word REST and the
concept in his doctoral thesis in 2000.
It’s an excellent choice of technology for use with mobile
applications and browser-based clients.
SOAP vs REST
●

●

●

●

●

●

●

●

SOAP is a XML based messaging protocol and REST is not a protocol but an
architectural style.
SOAP has a standard specification but there is none for REST.
Even SOAP based web services can be implemented in RESTful style. REST is a concept
that does not tie with any protocols.
REST does not enforces message format as XML or JSON or etc. But SOAP is XML
based message protocol.
REST follows stateless model. SOAP has specifications for stateful implementation as
well.
SOAP is strongly typed, has strict specification for every part of implementation. But
REST gives the concept and less restrictive about the implementation.
SOAP uses interfaces(WSDL) and named operations to expose business logic. REST uses
(generally) URI and methods like (GET, PUT, POST, DELETE) to expose resources.
REST only works over HTTP and HTTPS. SOAP works over HTTP, HTTPS, SMTP,
XMPP, etc.
What to Choose?
If you want a web service that is activity oriented or requires
additional level of security or secure messaging then SOAP service is
the best option to choose. Activity is much more than just CRUD
operations.
If you want a web service that is more of resource-oriented, need to
do some CRUD operation on a resource then REST service is the best
option to choose. Due to its light weight it is mostly preferred for
browser based and mobile based applications.
In general, when you're publishing an API to the outside world that
is either complex or likely to change, SOAP will be more useful. Other
than that, REST is usually the better option.
SOAP Webservice
1. First, create and define an Apex class as global. Then create and define an
Apex method using both the static and webservice modifiers as shown
below.
global class HelloWorld {
webService static string sayHello(){
return 'Hello';
}
}
The global access modifier declares that the class is visible to all Apex
scripts everywhere.
This means the class can be used by any Apex code, not just the Apex in the
same application.
SOAP Webservice(Continued...)
2. To access the webservice WSDL, go to Setup | App Setup | Develop | Apex
Classes, find the specific class where the web service is defined and click on
the WSDL hyperlink and download it.
3. Collect the Enterprise WSDL from Setup | App Setup | Develop | API by
clicking Generate Enterprise WSDL link.
The Partner and Enterprise WSDL have a login() method for which we
need it along with our custom webservice wsdl so that we can give the
username and password, get the session id, and then switch over to our
custom web service to make it work. Session ids are tied to a given user in the
organization, so we need to control what they can access by giving them their
own profile and locking down access to only what they need to get to.
Those are the WSDL files which are to be imported into the external
application(s) that will be invoking the web service.
SOAP Webservice(Continued...)
●

●

●

●

●
●

●

The webservice modifier can't be used on a class itself, or an interface or
interface methods or variables.
The @future annotation needs to be used to make the Apex method
execute asynchronously.
Asynchronous callouts can be made with triggers but synchronous callout
with trigger is not supported(use actionpoller to check and show if a
response has been received while other transactions were ongoing).
All classes or inner classes that contain methods or variables defined with
the webservice keyword must be declared as global.
You must define any method that uses the webservice keyword as static.
Methods defined with the webservice keyword cannot take the following
elements as parameters. While these elements can be used within the
method, they cannot be used 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.
Public SOAP Webservice
1) Create a SOAP webservice.
2) Go to Site > Setup > App Setup > Develop > Sites > Select your Site >
Give access of class "MerchandiseManager" to site profile
3) Extract WSDL of your class, go to your class and then click "Generate
WSDL". Now all we need to change the SOAP address location in the
generated WSDL soap:address tag location value.
Lets say this is the location:
https://ap1.salesforce.com/services/Soap/class/HelloWorld
And our site URL is
https://mindfire-surya-developer-edition.ap1.force.com/
So our final location will be:
https://mindfire-surya-developeredition.ap1.force.com/services/Soap/class/HelloWorld
SOAP Callout
Apex Callouts enable Apex to invoke external SOAP web services using
WSDL so that you connect to third party services.
Before any Apex callout can call an external site, that site must be
registered in the Remote Site Settings page, or the callout fails.
Skipping this will result in "System.CalloutException: IO Exception:
Unauthorized endpoint, please check Setup->Security->Remote site settings.
endpoint ="...
SOAP Callout(wsdl2apex)
1. Collect the WSDL for the application that the salesforce is going to
consume.
2. Import the WSDL into Salesforce
Develop > Apex Classes > Generate from WSDL.
3. The successfully generated Apex class includes methods for calling the
third-party Web service represented by the WSDL document. Now create an
instance of the stub in your Apex code and call the methods on generated
Apex class.
Some free SOAP webservices with WSDL can be found :
http://www.actionscript.org/forums/showthread.php3?t=70742
WSDL Parsing Errors
There is a list of Supported WSDL Features listed:

http://wiki.developerforce.com/page/Apex_Web_Services_and_Callouts#Supported_WSD

Beyond those you can modify the source WSDL to get a reasonable level of
support. For example:
1. WSDL with multiple portType, binding, port are not supported in Apex so
do keep only respective PortType, binding and port before generating class
from WSDL.
2. Datatype “anyType” is not supported in WSDLs used to generate Apex
code that is saved using API version 15.0 and later, change the same to
“string”.
3. …......
SOAP Callout(Continued...)
The WSDL2Apex generated code supports HTTP Headers. For example, you can use
this feature to set the value of a cookie in an authorization header. To set HTTP
headers, add inputHttpHeaders_x and outputHttpHeaders_x to the stub.
Here's an example that sets input HTTP Headers:
docSample.DocSamplePort stub = new docSample.DocSamplePort();
stub.inputHttpHeaders_x = new Map<String, String>();
//Setting a basic authentication header
stub.inputHttpHeaders_x.put('Authorization', 'Basic
QWxhZGRpbjpvcGVuIHNlc2FtZQ==');
//Setting a cookie header
stub.inputHttpHeaders_x.put('Cookie', 'name=value');
//Setting a custom HTTP header
stub.inputHttpHeaders_x.put('myHeader', 'myValue');
....
SOAP Callout(Continued...)
Here's one that accesses output HTTP Headers information:
docSample.DocSamplePort stub = new docSample.DocSamplePort();
stub.outputHttpHeaders_x = new Map<String, String>();
String input = 'This is the input string';
String output = stub.EchoString(input);
//Getting cookie header
String cookie = stub.outputHttpHeaders_x.get('Set-Cookie');
//Getting custom header
String myHeader = stub.outputHttpHeaders_x.get('My-Header');
Testing SOAP Callout
Apex provides the built-in WebServiceMock interface and the
Test.setMock method that we can use to receive fake responses in a
test method for a SOAP callout.

Security
Authentication(Certificate 2way SSL)
Authorization(SessionID or OAuth 2.0)
Crypto class
EncodingUtil Class
Certificate
Server Certificate
Client Certificate
- Legacy Process
stub.clientCert_x = 'xyz....';
stub.clientCertPasswd_x = 'passwd'; // <<< Password for the keystore
-Salesforce Org
stub.clientCertName_x = 'Certificate Name'; // <<< Salesforce’s certificate name
When SFDC makes a call to a secured server, it first tries to see if the server has a
Server Certificate. If it does, then it sends the Client Certificate. If this Client
Certificate is accepted by the web server, then the communication is valid and the
data is sent to and fro between SFDC and the web server
Oauth 2.0
OAuth endpoints are the URLs you use to make OAuth authentication
requests to Salesforce.
You need to use the correct Salesforce OAuth endpoint when issuing
authentication requests in your application. The primary OAuth endpoints
are:
For authorization: https://login.salesforce.com/services/oauth2/authorize
For token requests: https://login.salesforce.com/services/oauth2/token
For revoking OAuth tokens:
https://login.salesforce.com/services/oauth2/revoke
All endpoints require secure HTTP (HTTPS). Each OAuth flow defines
which endpoints you need to use and what request data you need to provide.
-Web Server Agent Oauth
-User Agent Oauth
-Username Password Agent OAuth
References
http://wiki.developerforce.com/page/Apex_Web_Services_and_Callouts
http://forceguru.blogspot.in/2012/09/creating-public-web-service-in.html
http://blog.deadlypenguin.com/blog/2012/02/03/salesforce-and-soapui/
http://kperisetla.blogspot.in/2012/05/restful-services-on-forcecom-through.html
http://www.fishofprey.com/2011/03/consuming-aspnet-web-service-from.html
http://www.salesforce.com/us/developer/docs/apexcode/index.htm

http://stackoverflow.com/questions/209905/representational-state-transfer-rest-and-simple-object-ac
http://www.salesforce.com/us/developer/docs/api_rest/index_Left.html

http://blog.deadlypenguin.com/blog/2012/04/13/salesforce-and-soapui-using-the-default-query-meth
http://medbiq.org/std_specs/techguidelines/knowingwhentorest.pdf
http://www.developingthefuture.net/web-services-overview/
http://www.tgerm.com/2010/12/invoking-apex-wsdl-web-services-from.html

http://blogs.developerforce.com/tech-pubs/2011/10/salesforce-apis-what-they-are-when-to-use-them
Question and Answers
Thank You

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Support JEE Spring Inversion de Controle IOC et Spring MVC
Support JEE Spring Inversion de Controle IOC et Spring MVCSupport JEE Spring Inversion de Controle IOC et Spring MVC
Support JEE Spring Inversion de Controle IOC et Spring MVC
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
Spring Security 5
Spring Security 5Spring Security 5
Spring Security 5
 
Chp2 - Vers les Architectures Orientées Services
Chp2 - Vers les Architectures Orientées ServicesChp2 - Vers les Architectures Orientées Services
Chp2 - Vers les Architectures Orientées Services
 
eServices-Chp2: SOA
eServices-Chp2: SOAeServices-Chp2: SOA
eServices-Chp2: SOA
 
Java Server Faces (JSF)
Java Server Faces (JSF)Java Server Faces (JSF)
Java Server Faces (JSF)
 
eServices-Chp4: ESB
eServices-Chp4: ESBeServices-Chp4: ESB
eServices-Chp4: ESB
 
Inversion of Control and Dependency Injection
Inversion of Control and Dependency InjectionInversion of Control and Dependency Injection
Inversion of Control and Dependency Injection
 
The Art of Discovering Bounded Contexts
The Art of Discovering Bounded ContextsThe Art of Discovering Bounded Contexts
The Art of Discovering Bounded Contexts
 
Java EE Introduction
Java EE IntroductionJava EE Introduction
Java EE Introduction
 
Implementing Microservices Security Patterns & Protocols with Spring
Implementing Microservices Security Patterns & Protocols with SpringImplementing Microservices Security Patterns & Protocols with Spring
Implementing Microservices Security Patterns & Protocols with Spring
 
Introduction to APIs (Application Programming Interface)
Introduction to APIs (Application Programming Interface) Introduction to APIs (Application Programming Interface)
Introduction to APIs (Application Programming Interface)
 
An Overview of Web Services: SOAP and REST
An Overview of Web Services: SOAP and REST An Overview of Web Services: SOAP and REST
An Overview of Web Services: SOAP and REST
 
Toi uu hoa he thong 30 trieu nguoi dung
Toi uu hoa he thong 30 trieu nguoi dungToi uu hoa he thong 30 trieu nguoi dung
Toi uu hoa he thong 30 trieu nguoi dung
 
Restful web services ppt
Restful web services pptRestful web services ppt
Restful web services ppt
 
Support Web Services SOAP et RESTful Mr YOUSSFI
Support Web Services SOAP et RESTful Mr YOUSSFISupport Web Services SOAP et RESTful Mr YOUSSFI
Support Web Services SOAP et RESTful Mr YOUSSFI
 
Introduction to the Web API
Introduction to the Web APIIntroduction to the Web API
Introduction to the Web API
 
Les Servlets et JSP
Les Servlets et JSPLes Servlets et JSP
Les Servlets et JSP
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
 
Introduction to microservices
Introduction to microservicesIntroduction to microservices
Introduction to microservices
 

Ähnlich wie Webservices in SalesForce (part 1)

Xml web services
Xml web servicesXml web services
Xml web services
Raghu nath
 
Create Home Directories on Storage Using WFA and ServiceNow integration
Create Home Directories on Storage Using WFA and ServiceNow integrationCreate Home Directories on Storage Using WFA and ServiceNow integration
Create Home Directories on Storage Using WFA and ServiceNow integration
Rutul Shah
 
Web service through cxf
Web service through cxfWeb service through cxf
Web service through cxf
Roger Xia
 
Exchange of data over internet using web service(e.g., soap and rest) in SAS ...
Exchange of data over internet using web service(e.g., soap and rest) in SAS ...Exchange of data over internet using web service(e.g., soap and rest) in SAS ...
Exchange of data over internet using web service(e.g., soap and rest) in SAS ...
Kevin Lee
 
Creating Web Services with Zend Framework - Matthew Turland
Creating Web Services with Zend Framework - Matthew TurlandCreating Web Services with Zend Framework - Matthew Turland
Creating Web Services with Zend Framework - Matthew Turland
Matthew Turland
 

Ähnlich wie Webservices in SalesForce (part 1) (20)

Web services in java
Web services in javaWeb services in java
Web services in java
 
JavaEE6 my way
JavaEE6 my wayJavaEE6 my way
JavaEE6 my way
 
Xml web services
Xml web servicesXml web services
Xml web services
 
Express node js
Express node jsExpress node js
Express node js
 
Create Home Directories on Storage Using WFA and ServiceNow integration
Create Home Directories on Storage Using WFA and ServiceNow integrationCreate Home Directories on Storage Using WFA and ServiceNow integration
Create Home Directories on Storage Using WFA and ServiceNow integration
 
Major project report
Major project reportMajor project report
Major project report
 
Servlets
ServletsServlets
Servlets
 
Rest web service
Rest web serviceRest web service
Rest web service
 
MuleSoft London Community February 2020 - MuleSoft and OData
MuleSoft London Community February 2020 - MuleSoft and ODataMuleSoft London Community February 2020 - MuleSoft and OData
MuleSoft London Community February 2020 - MuleSoft and OData
 
Web service through cxf
Web service through cxfWeb service through cxf
Web service through cxf
 
Exchange of data over internet using web service(e.g., soap and rest) in SAS ...
Exchange of data over internet using web service(e.g., soap and rest) in SAS ...Exchange of data over internet using web service(e.g., soap and rest) in SAS ...
Exchange of data over internet using web service(e.g., soap and rest) in SAS ...
 
Xamarin Workshop Noob to Master – Week 5
Xamarin Workshop Noob to Master – Week 5Xamarin Workshop Noob to Master – Week 5
Xamarin Workshop Noob to Master – Week 5
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083
 
Web API or WCF - An Architectural Comparison
Web API or WCF - An Architectural ComparisonWeb API or WCF - An Architectural Comparison
Web API or WCF - An Architectural Comparison
 
Frequently asked MuleSoft Interview Questions and Answers from Techlightning
Frequently asked MuleSoft Interview Questions and Answers from TechlightningFrequently asked MuleSoft Interview Questions and Answers from Techlightning
Frequently asked MuleSoft Interview Questions and Answers from Techlightning
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing
 
Creating Web Services with Zend Framework - Matthew Turland
Creating Web Services with Zend Framework - Matthew TurlandCreating Web Services with Zend Framework - Matthew Turland
Creating Web Services with Zend Framework - Matthew Turland
 
Web services soap and rest by mandakini for TechGig
Web services soap and rest by mandakini for TechGigWeb services soap and rest by mandakini for TechGig
Web services soap and rest by mandakini for TechGig
 
Build Message-Based Web Services for SOA
Build Message-Based Web Services for SOABuild Message-Based Web Services for SOA
Build Message-Based Web Services for SOA
 
Rest web services
Rest web servicesRest web services
Rest web services
 

Mehr von Mindfire Solutions

Mehr von Mindfire Solutions (20)

Physician Search and Review
Physician Search and ReviewPhysician Search and Review
Physician Search and Review
 
diet management app
diet management appdiet management app
diet management app
 
Business Technology Solution
Business Technology SolutionBusiness Technology Solution
Business Technology Solution
 
Remote Health Monitoring
Remote Health MonitoringRemote Health Monitoring
Remote Health Monitoring
 
Influencer Marketing Solution
Influencer Marketing SolutionInfluencer Marketing Solution
Influencer Marketing Solution
 
ELMAH
ELMAHELMAH
ELMAH
 
High Availability of Azure Applications
High Availability of Azure ApplicationsHigh Availability of Azure Applications
High Availability of Azure Applications
 
IOT Hands On
IOT Hands OnIOT Hands On
IOT Hands On
 
Glimpse of Loops Vs Set
Glimpse of Loops Vs SetGlimpse of Loops Vs Set
Glimpse of Loops Vs Set
 
Oracle Sql Developer-Getting Started
Oracle Sql Developer-Getting StartedOracle Sql Developer-Getting Started
Oracle Sql Developer-Getting Started
 
Adaptive Layout In iOS 8
Adaptive Layout In iOS 8Adaptive Layout In iOS 8
Adaptive Layout In iOS 8
 
Introduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/MacIntroduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/Mac
 
LINQPad - utility Tool
LINQPad - utility ToolLINQPad - utility Tool
LINQPad - utility Tool
 
Get started with watch kit development
Get started with watch kit developmentGet started with watch kit development
Get started with watch kit development
 
Swift vs Objective-C
Swift vs Objective-CSwift vs Objective-C
Swift vs Objective-C
 
Material Design in Android
Material Design in AndroidMaterial Design in Android
Material Design in Android
 
Introduction to OData
Introduction to ODataIntroduction to OData
Introduction to OData
 
Ext js Part 2- MVC
Ext js Part 2- MVCExt js Part 2- MVC
Ext js Part 2- MVC
 
ExtJs Basic Part-1
ExtJs Basic Part-1ExtJs Basic Part-1
ExtJs Basic Part-1
 
Spring Security Introduction
Spring Security IntroductionSpring Security Introduction
Spring Security Introduction
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 

Webservices in SalesForce (part 1)

  • 1. Webservices In Salesforce (Part 1) Presenter: Surya Kanta Mekap, Mindfire Solutions Email: suryam@mindfiresolutions.com SkypeId: mfsi_suryam Date: 4 Oct 2013
  • 2. Overview 1. Introduction 2. What is SOAP? 3. What is REST? 4. SOAP vs REST 5. What to Choose? 6. SOAP Webservice 7. Public SOAP Webservice 8. SOAP Callout 9. Testing Webservice Callout 10. Summary
  • 3. Webservice? Webservice is a sofware function or method of an application exposed to the outside world allowing other application to invoke it from the web.
  • 4. What is SOAP? The Simple Object Access Protocol(SOAP) web service is a architecture pattern, which specifies the basic rules to be considered while designing web service platforms. The SOAP message itself consists of an envelope, inside of which are the SOAP headers and body, the actual information we want to send. It is based on the standard XML format, designed especially to transport and store structured data. SOAP may also refer to the format of the XML that the envelope uses. Best for activity oriented services. An activity is more than just insert or update or delete a record.
  • 5. What is REST? Representational State Transfer(REST) is another architectural pattern. Unlike SOAP, RESTful applications use the GET, POST, PUT and DELETE to perform CRUD operations. REST is resource-oriented and uses URI (or RESTful URLs). Roy Fielding is the Man who introduced the word REST and the concept in his doctoral thesis in 2000. It’s an excellent choice of technology for use with mobile applications and browser-based clients.
  • 6. SOAP vs REST ● ● ● ● ● ● ● ● SOAP is a XML based messaging protocol and REST is not a protocol but an architectural style. SOAP has a standard specification but there is none for REST. Even SOAP based web services can be implemented in RESTful style. REST is a concept that does not tie with any protocols. REST does not enforces message format as XML or JSON or etc. But SOAP is XML based message protocol. REST follows stateless model. SOAP has specifications for stateful implementation as well. SOAP is strongly typed, has strict specification for every part of implementation. But REST gives the concept and less restrictive about the implementation. SOAP uses interfaces(WSDL) and named operations to expose business logic. REST uses (generally) URI and methods like (GET, PUT, POST, DELETE) to expose resources. REST only works over HTTP and HTTPS. SOAP works over HTTP, HTTPS, SMTP, XMPP, etc.
  • 7. What to Choose? If you want a web service that is activity oriented or requires additional level of security or secure messaging then SOAP service is the best option to choose. Activity is much more than just CRUD operations. If you want a web service that is more of resource-oriented, need to do some CRUD operation on a resource then REST service is the best option to choose. Due to its light weight it is mostly preferred for browser based and mobile based applications. In general, when you're publishing an API to the outside world that is either complex or likely to change, SOAP will be more useful. Other than that, REST is usually the better option.
  • 8. SOAP Webservice 1. First, create and define an Apex class as global. Then create and define an Apex method using both the static and webservice modifiers as shown below. global class HelloWorld { webService static string sayHello(){ return 'Hello'; } } The global access modifier declares that the class is visible to all Apex scripts everywhere. This means the class can be used by any Apex code, not just the Apex in the same application.
  • 9. SOAP Webservice(Continued...) 2. To access the webservice WSDL, go to Setup | App Setup | Develop | Apex Classes, find the specific class where the web service is defined and click on the WSDL hyperlink and download it. 3. Collect the Enterprise WSDL from Setup | App Setup | Develop | API by clicking Generate Enterprise WSDL link. The Partner and Enterprise WSDL have a login() method for which we need it along with our custom webservice wsdl so that we can give the username and password, get the session id, and then switch over to our custom web service to make it work. Session ids are tied to a given user in the organization, so we need to control what they can access by giving them their own profile and locking down access to only what they need to get to. Those are the WSDL files which are to be imported into the external application(s) that will be invoking the web service.
  • 10. SOAP Webservice(Continued...) ● ● ● ● ● ● ● The webservice modifier can't be used on a class itself, or an interface or interface methods or variables. The @future annotation needs to be used to make the Apex method execute asynchronously. Asynchronous callouts can be made with triggers but synchronous callout with trigger is not supported(use actionpoller to check and show if a response has been received while other transactions were ongoing). All classes or inner classes that contain methods or variables defined with the webservice keyword must be declared as global. You must define any method that uses the webservice keyword as static. Methods defined with the webservice keyword cannot take the following elements as parameters. While these elements can be used within the method, they cannot be used 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.
  • 11. Public SOAP Webservice 1) Create a SOAP webservice. 2) Go to Site > Setup > App Setup > Develop > Sites > Select your Site > Give access of class "MerchandiseManager" to site profile 3) Extract WSDL of your class, go to your class and then click "Generate WSDL". Now all we need to change the SOAP address location in the generated WSDL soap:address tag location value. Lets say this is the location: https://ap1.salesforce.com/services/Soap/class/HelloWorld And our site URL is https://mindfire-surya-developer-edition.ap1.force.com/ So our final location will be: https://mindfire-surya-developeredition.ap1.force.com/services/Soap/class/HelloWorld
  • 12. SOAP Callout Apex Callouts enable Apex to invoke external SOAP web services using WSDL so that you connect to third party services. Before any Apex callout can call an external site, that site must be registered in the Remote Site Settings page, or the callout fails. Skipping this will result in "System.CalloutException: IO Exception: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint ="...
  • 13. SOAP Callout(wsdl2apex) 1. Collect the WSDL for the application that the salesforce is going to consume. 2. Import the WSDL into Salesforce Develop > Apex Classes > Generate from WSDL. 3. The successfully generated Apex class includes methods for calling the third-party Web service represented by the WSDL document. Now create an instance of the stub in your Apex code and call the methods on generated Apex class. Some free SOAP webservices with WSDL can be found : http://www.actionscript.org/forums/showthread.php3?t=70742
  • 14. WSDL Parsing Errors There is a list of Supported WSDL Features listed: http://wiki.developerforce.com/page/Apex_Web_Services_and_Callouts#Supported_WSD Beyond those you can modify the source WSDL to get a reasonable level of support. For example: 1. WSDL with multiple portType, binding, port are not supported in Apex so do keep only respective PortType, binding and port before generating class from WSDL. 2. Datatype “anyType” is not supported in WSDLs used to generate Apex code that is saved using API version 15.0 and later, change the same to “string”. 3. …......
  • 15. SOAP Callout(Continued...) The WSDL2Apex generated code supports HTTP Headers. For example, you can use this feature to set the value of a cookie in an authorization header. To set HTTP headers, add inputHttpHeaders_x and outputHttpHeaders_x to the stub. Here's an example that sets input HTTP Headers: docSample.DocSamplePort stub = new docSample.DocSamplePort(); stub.inputHttpHeaders_x = new Map<String, String>(); //Setting a basic authentication header stub.inputHttpHeaders_x.put('Authorization', 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='); //Setting a cookie header stub.inputHttpHeaders_x.put('Cookie', 'name=value'); //Setting a custom HTTP header stub.inputHttpHeaders_x.put('myHeader', 'myValue'); ....
  • 16. SOAP Callout(Continued...) Here's one that accesses output HTTP Headers information: docSample.DocSamplePort stub = new docSample.DocSamplePort(); stub.outputHttpHeaders_x = new Map<String, String>(); String input = 'This is the input string'; String output = stub.EchoString(input); //Getting cookie header String cookie = stub.outputHttpHeaders_x.get('Set-Cookie'); //Getting custom header String myHeader = stub.outputHttpHeaders_x.get('My-Header');
  • 17. Testing SOAP Callout Apex provides the built-in WebServiceMock interface and the Test.setMock method that we can use to receive fake responses in a test method for a SOAP callout. Security Authentication(Certificate 2way SSL) Authorization(SessionID or OAuth 2.0) Crypto class EncodingUtil Class
  • 18. Certificate Server Certificate Client Certificate - Legacy Process stub.clientCert_x = 'xyz....'; stub.clientCertPasswd_x = 'passwd'; // <<< Password for the keystore -Salesforce Org stub.clientCertName_x = 'Certificate Name'; // <<< Salesforce’s certificate name When SFDC makes a call to a secured server, it first tries to see if the server has a Server Certificate. If it does, then it sends the Client Certificate. If this Client Certificate is accepted by the web server, then the communication is valid and the data is sent to and fro between SFDC and the web server
  • 19. Oauth 2.0 OAuth endpoints are the URLs you use to make OAuth authentication requests to Salesforce. You need to use the correct Salesforce OAuth endpoint when issuing authentication requests in your application. The primary OAuth endpoints are: For authorization: https://login.salesforce.com/services/oauth2/authorize For token requests: https://login.salesforce.com/services/oauth2/token For revoking OAuth tokens: https://login.salesforce.com/services/oauth2/revoke All endpoints require secure HTTP (HTTPS). Each OAuth flow defines which endpoints you need to use and what request data you need to provide. -Web Server Agent Oauth -User Agent Oauth -Username Password Agent OAuth
  • 20. References http://wiki.developerforce.com/page/Apex_Web_Services_and_Callouts http://forceguru.blogspot.in/2012/09/creating-public-web-service-in.html http://blog.deadlypenguin.com/blog/2012/02/03/salesforce-and-soapui/ http://kperisetla.blogspot.in/2012/05/restful-services-on-forcecom-through.html http://www.fishofprey.com/2011/03/consuming-aspnet-web-service-from.html http://www.salesforce.com/us/developer/docs/apexcode/index.htm http://stackoverflow.com/questions/209905/representational-state-transfer-rest-and-simple-object-ac http://www.salesforce.com/us/developer/docs/api_rest/index_Left.html http://blog.deadlypenguin.com/blog/2012/04/13/salesforce-and-soapui-using-the-default-query-meth http://medbiq.org/std_specs/techguidelines/knowingwhentorest.pdf http://www.developingthefuture.net/web-services-overview/ http://www.tgerm.com/2010/12/invoking-apex-wsdl-web-services-from.html http://blogs.developerforce.com/tech-pubs/2011/10/salesforce-apis-what-they-are-when-to-use-them