Showing posts with label JSON Parsing. Show all posts
Showing posts with label JSON Parsing. Show all posts

Wednesday, August 23, 2017

How to Invoke the REST API from the Visual Force Page in Salesforce

Problem:
We usually making a lot of API calls from Salesforce to External systems to get the some data or to pass the some updates to External Systems.All these calls will be made from the Apex class to External systems. What if you have situation like where you need to make a call to external systems from the visual force page.

Solution:
No need to worry still we have option to make an REST API call from the page directly.How?As we already know that Salesforce has introduced a concept called AJAX ToolKit.This is an frame which will be used to make all the operations from JavaScript similar to APEX. With help of this tool kit you can make an REST API call from the Script in Visual Force Page

Flow Diagram:
 

Sample Source Code:

<apex:page standardController="Account">
<apex:includeScript value="//code.jquery.com/jquery-1.11.1.min.js" />
<script src="../../soap/ajax/40.0/connection.js" type="text/javascript"></script>

<script>
function makeAPICall() 
{
    var weblink = //Your API endpoint 'https://www.yourednpoint/';
 
    $j.ajax({
  url: weblink,
  type: 'GET', // Type POST or GET
  dataType: 'json',
  beforeSend: function(request) {
      // Add All your Headers Here if Any
   request.setRequestHeader('HeaderString','HeaderValue');
   //Sample Headers 
   request.setRequestHeader('Country', 'IND');
   request.setRequestHeader('Currency', 'INR');
  },

  crossDomain: true,
  
  //If Successfully executed 
  success: function(result) {
   
   //Response will be stored in result variable in the form of Object.Please use result variable for processing
    console.log('Response Result'+result); 
    
      //If you want to convert JSResponse Object to JSON response 
    var jsonResp = JSON.stringify(result));

  },
  
  //If any Error
  
  error: function(jqXHR, textStatus, errorThrown) {
   
   // alert('ErrorThrown: ' + errorThrown);
  }
    });
}
</script>
<apex:form>
  call javaScript method from here to make an api call invocation
</apex:form>
</apex:page>

Hope this helps you....Enjoy!
 

Friday, August 4, 2017

Parse the JSON Response in Salesforce Apex -Part 2

In this post I'm going to explain how to create a wrapper class in apex to parse the JSON response which having multiple key value pairs only.

JSON String:


{
    "AccID1" : {
        "contacts" : {
            "0055be4ce5802020100" : "CREATED",
            "0055be4ce5802020100" : "Open"
        },
  "cs" : {
            "f0055be4ce5802020100" : "CREATED",
            "09055be4ce5802020100" : "Completed"
        }
    },
    "AccID2" : {
        "contacts" : {
            "0055be4ce5802020100" : "SUCCESSFUL"
        }
    },
    "AccID3" : {
        "contacts" : {
            "0055be4ce5801020100" : "CREATED"
        },
        "cs" : {
            "f0055be4ce5802030100" : "Open",
            "f0055be4ce5802030100" : "Re-Open",
            "f0055be4ce5802030100" : "Closed"
        }
    },
 "AccID4" : {
        "contacts" : {
            "0055be4ce5801020100" : "Initiated"
        }
       
    }
}

Apex Class:

//Wrapper Class For Parsering 
public class ParsingTest
{
  public ParsingTest()
  {
        String jsonString='Pass your JSON String Here';
 Map<string,ContactsCSWrapper> apiRespone = new Map<string,ContactsCSWrapper>();
 apiRespone = (Map<string,ContactsCSWrapper>)System.JSON.deserialize(jsonString,Map<string,ContactsCSWrapper>.class);
 System.debug('apiRespone..'+apiRespone.keySet()); //Outputs AccID1,AccID2,AccID3,AccID4
  }
  
  public class ContactsCSWrapper 
 {
  public map<string, string> cs { get; set; }
  public map<string, string> contacts { get; set; }
 }
}


Hope this helps you....Enjoy!

Parse the JSON Response in Salesforce Apex -Part 1

In this post I'm going to explain how to create a Wrapper class to Parse the JSON Response in Apex which contains key and value pair in JSON.

JSON String:

{
 "status": "SUCCESS",
 "errors": [],
 "data": {
  "oldCaseStatus": "Case_CANCELLATION_success",
  "newCaseStatus": "Case_CANCELLATION_success",
  "NewCaseNumber": "3732gy12"
         }
}

Apex Class:

public class ParsingTest
{
  public ParsingTest()
  {
      String json='{"status":"SUCCESS","errors":[],"data":{"oldCaseStatus":"Case_CANCELLATION_success","newCaseStatus":"Case_CANCELLATION_success","NewCaseNumber":"3732gy12"}}';
      ParsingWrapper apiRespone = new ParsingWrapper();
      
      apiRespone = (ParsingWrapper)System.JSON.deserialize(json,ParsingWrapper.class);
      System.debug('apiRespone..'+apiRespone);
  }

 //Wrapper Class For Parsering 

  public class ParsingWrapper
  {
   public String status{get;set;}
   public Map<String,String> data{get;set;}
   public Cls_errors[] errors;
  }

  public class Cls_errors
  {

  }
}


Hope this helps you....Enjoy!