Wednesday, May 10, 2017

Sample example for consuming Rest Web Service (RestWSSampleWithParam) containing web method with parameters from same Salesforce ORG.

Scenario: Suppose we have a rest web service in ORG1 (As posted in previous post named as "RestWSSampleWithParam"). This service has a web method (getRestSampleResponse) which contains 2 parameters. 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 (RestWSSampleWithParam) from same ORG with parameters.
 **/
public class RestWSWithParamsConsumeFromSameOrg
{
  public static void callingRestWS(){
    try {
      // Setup remote site setting for the below URL.
          String url = 'https://ap5.salesforce.com/services/apexrest/RestWSSampleParamExmpl';
          system.debug('Response URL'  +url);   
          
          HttpRequest req = new HttpRequest();
          req.setMethod('POST');
          //req.setHeader('content-type', 'application/json');
          req.setHeader('Content-Type', 'application/xml; charset=utf-8');
          req.setEndpoint(url);   
          req.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
          req.setBody('<?xml version="1.0" encoding="UTF-8" ?><request><firstName>Arun</firstName><lastName>Hazra</lastName></request>');
    
          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());
    }
  }
}

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