Monday, 17 April 2017

Interview Questions on REST API and SOAP API in salesforce

1.What is SOAP and What is REST?


Representational State Transfer.
It is based URI
Works Over with HTTP and HTTPS


Simple Object Access Protocol.
It is based on Standard XML format
It is works with WSDL

2.Difference between REST API and SOAP API?
Ans :Varies on records that can be handled. Generally if we want to access less number of records we go for REST API.
3.What is WSDL?
A WSDL is an XML Document which contains a standardized description of how to communicate using webservice.
4.What are the different type of WSDL'S?
Enterprise WSDL
Partner WSDL
Metadata WSDL
Tooling WSDL
Delegated Atuntection WSDL
5.Difference between Enterprise WSDL and Partner WSDL?
Enterprise WSDL:
It is used for building client applications for a single salesforce organization.
Customers who use enterprise WSDL document must download and re-consume it when ever their organization makes a change to its custom objects or fields or when ever they want to use a different version of the API.
Partner WSDL:
It is used for building client applications for multiple organizations.
The partner WSDL documention only needs to be downloaded consumed once per version of the API.
6.How can you expose an apex class as a REST web service in salesforce?
Ans - An apex class can be exposed as REST web service by using keyword '@RestResource'
7.How to fetch data from another Salesforce instance using API?
Ans :Use the Web Services API or Bulk API to transfer data We this this is a great job for the Bulk API.
8.How to call Apex method from a Custom Button?
Ans :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 Apex code and then receiving the response.
Apex provides integration with Web services that utilize SOAP and WSDL, or HTTP services (RESTful services).
9.What is the use of Chatter REST API?
The Chatter API (also called Chatter REST API) lets you access Chatter information via an optimized REST-based API accessible from any platform. Developers can now build social applications for mobile devices, or highly interactive websites, quickly and efficiently.
10.How to fetch data from another Salesforce instance using API?
Answer: Use the FORCE.COM WEB SERVICES API or BULK API to transfer data We this this is a great job for the Bulk API.
11.What are callouts and call ins?
Making a request to an external system from salsforce is callout.

Getting requests from an external system is a call in.
12.How many callouts to external service can be made in a single apex transaction?
Ans - A total of 10 callouts are allowed in a single apex transaction.
13.What is the maximum allowed time limit while making a callout to external service in apex?
Ans - maximum of 120 second time limit is enforced while making callout to external service
14.What is the default timeout period while calling webservice from Apex.
Ans : 10 sec.
15.can we define custom time out for each call out?
Ans :
A custom time time can be defined for each callout.
the minimum time is 1 millisecond and maximum is 120,000 milli seconds.
16.How to increase timeout while calling web service from Apex ?
Ans :
docSample.DocSamplePort stub = new docSample.DocSamplePort();

Salesforce one org to another salesforce org integration
>One of the ways to Integrate two Salesforce Instances will be using oauth for session management and using REST API

>Lets Assume you have SalesforceA as Source ORG and SalesforceB as Destination ORG .Now lets assume flow of data is from SalesforceA to SalesforceB Instance

>1)For oauth 2.0 in Destination org(SalesforceB) create a Connected App .To help you configure the Connected App in Destination below screenshot should assist.Callback Url may not be of any significant as we are using User-Name Password flow model.You will obtain client secret,consumer key in this process and one should store in Source org custom object or custom setting

![enter image description here][1]

>2)In your source org create a Remote site settings with url as the URL of the Destination Instance

>3)Create a Integration User in the Destination org whose credentials you will use to call API from Salesforce Source ORG

>4)Use the username,password and security token(Also i would prefer setting password never expires here for integration User) and also client secret and consumer key in a custom object or custom setting (Protected) in Source org.

The below code is sample code to get authenticated and how to use access token to further make any API call

    Settings__c ts=settings;//Here write a sample query or fetch from custom settings the consumer ,client secret and username and password of destination org
    String clientId = ts.ConsumerKey__c;
    String clientSecret = ts.Client_Secret__c;
    String username=ts.Username__c;
    String password=ts.Password__c+ts.SecurityToken__c;
    String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint(ts.URL__c+'/services/oauth2/token');//Note if my domain is set up use the proper domain name else use for prod or developer or for sandbox instance

    HttpResponse res = h.send(req);
    OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class);
    RequestWrapper reqst=new RequestWrapper();
      Http h1 = new Http();
      HttpRequest req1 = new HttpRequest();
      req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
      req1.setBody(jsonstr);//Send JSON body
      req1.setEndpoint(ts.URL__c+URL);//URL will be your Salesforce REST API end point where you will do POST,PUT,DELETE orGET
      HttpResponse res1 = h1.send(req1);
      system.debug('RESPONSE_BODY'+res1 .getbody());
>To deserialize the initial response here is wrapper class the above code uses

    /*To get aouthentication detail Wrapper*/
    public class OAuth2{
     public String id{get;set;}
     public String issued_at{get;set;}
     public String instance_url{get;set;}
     public String signature{get;set;}
     public String access_token{get;set;}   

Post a Comment