Wednesday, May 10, 2017

Sample example for consuming Rest Web Service (RestWSSample) from one Salesforce ORG to another.

Scenario: Suppose we have a rest web service in ORG1 (As posted in previous post named as "RestWSSample"). Now our aim is to call the web service from different ORG (ORF2).

Solution: Below is the steps and apex code example for consuming rest web service from different Org.

ORG1 Activity:
1. Login to ORG1, where your web service is written.
2. Go to Setup > Create > Apps
3. Click on Apps will open Apps detail page. From this page click on "New" button under "Connected Apps" section.
4. In a new page enter the details.

5. Once it is saved, then in detail page it will auto create below 2 details,
i> Consumer Key,
ii> Consumer Secret

ORG2 Activity:
1. Login to ORG2, from where ORG1 web service will be called.
2. Setup remote site setting.
3. Now copy past the below apex code to call the ORG1 rest web service.


/**
 * Consuming Rest Web Service (RestWSSample) from different ORG.
 **/
public class RestWSConsumeFromDifferentOrg
{
    String endPointUrl = 'https://ap5.salesforce.com/services/apexrest/RestWSSampleExmpl';   
    
    public void callingRestWS(){
        HttpRequest req = new HttpRequest();
        string EndPt = endPointUrl;
        
        // Set request header to send token, setMethod, and set Endpoint
        req.setHeader('Authorization','Bearer '+ getAccessToken());        
        req.setMethod('GET');
        req.setEndpoint(EndPt);
        
        Http http = new Http();        
        //finally make a call
        HttpResponse res = http.send(req);
        
        system.debug('RESPONSE_BODY'+res.getbody());
    }
    
    private String getAccessToken(){
        String retVal = null;
        
        // This four variables are required for authentication to Oauth Token for ORG2 where WS is exist.
  // Consumer Key
  String consumerKey = 'Consumer Key created on ORG1 connected App' ;
        // Consumer Secret
  String consumerSecret = 'Consumer Secret created on ORG1 connected App';
        String username= 'ORG1 user name';
        String password= 'ORG1 password';
        
        //using this request body we'll make API call
        String reqbody = 'grant_type=password&client_id='+consumerKey+'&client_secret='
                        +consumerSecret+'&username='+username+'&password='+password;
    
        HttpRequest req = new HttpRequest();
        req.setBody(reqbody);
        req.setMethod('GET');
        req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
        
        Http http = new Http();
        HttpResponse res = http.send(req);
        
        OAuthWrapperClass objAuthenticationInfo = (OAuthWrapperClass)JSON.deserialize(res.getbody(), OAuthWrapperClass.class);
        
        if(objAuthenticationInfo != null && objAuthenticationInfo.access_token != null){
            retVal = objAuthenticationInfo.access_token;
        }
        
        return retVal;
    }
    
 //Wrapper class for OAuth details.
    private class OAuthWrapperClass{
        private String instance_url;
        private String access_token;
        private String Id;
        private String token_type;
        private String signature;
        private String issued_at;                
    }
}

Output: If every thing is OK, then while calling the callingRestWS() method from an anonymous window  will printing the response in debug message.

LWC to LWC Communication using Lightning Messaging Service (Part - 2)

In my previous post ( previous post link ) we have learn how to create a Lightning Messaging Service with 6 steps and today we will use the ...