Wednesday, May 10, 2017

Sample example for consuming Rest Web Service (RestWSSample) from same Salesforce ORG.

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 same ORG (ORF1).

Solution: Below is the apex code example for consuming rest web service from same Org.
Pre-step: Before copy paste the below code base, do the remote site setting for the endPoint URL.


/**
 * Consuming Rest Web Service (RestWSSample) from same ORG.
 **/
public class RestWSConsumeFromSameOrg
{
 public static void callingRestWS(){
  try {
   // Setup remote site setting for the below URL.
         String url = 'https://ap5.salesforce.com/services/apexrest/RestWSSampleExmpl';
         system.debug('Response URL'  +url);   
         
         HttpRequest req = new HttpRequest();
         req.setMethod('GET');
         req.setHeader('content-type', 'application/json');
         req.setEndpoint(url);   
         req.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());

         Http http = new Http();
         
         HTTPResponse resp = http.send(req);
         
         system.debug('Response body'  +resp.getBody()); 
         system.debug('STATUS: '+ resp.getStatus());
         system.debug('STATUS_CODE:'+ resp.getStatusCode());    
  } catch(System.CalloutException e){
   System.debug('Sorry, You have an error ' + e.getMessage());
  }
 }
}


Output: If every thing is OK, then while calling the callingRestWS() method from an anonymous window  will pring the debug message and for debug message 'Response body' it will display the below debug message,
"Response body Success Response."

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 ...