+ Start a Discussion
Nirav_ShahNirav_Shah 

Fetch Response from API and Show it on Visualforce Page

Hi Folks,

I have created one controller in which I am getting the API response in JSON format. The JSON format is as following
{
    "APIResponse": {
        "APIResult": {
            "row": [
                {
                    "SR_NBR": "1-233391976021",
                    "SR_CREATED": "2018-04-26T13:44:25.000Z",
                    "SR_TYPE": "Repair",
                    .
					.
					.
					.
					.
					.
					.
                }
            ]
        }
    }
}
Now, my requirement is I need to show this response in proper PDF format in Visualforce page.
How to achieve this scenario?

Thanks
Raj VakatiRaj Vakati
You need to parse the json and show on the page ..  GIve me the complete code I can able to rewrite it for you


https://github.com/Trigger2991/SFDC-JSON-Parser
https://wedgecommerce.com/json-parsing-salesforce/
http://blog.adityanaag.com/22/Parsing+JSON+the+easy+way+using+Apex
https://stackoverflow.com/questions/46579691/salesforce-parsing-json-with-apex-using-rest-callout-and-json2apex
https://opfocus.com/json-deserialization-techniques-in-salesforce/
 
Nirav_ShahNirav_Shah
Hi, how can I show the response on the VF page? Following is the Code

Controller :
public with sharing class ServiceReportCallout{

    public static Boolean serviceReportShowPDF{get;set;}
    public static String sUrl;
	public ServiceReportWrapper ServiceReportWrap { get; set;}
    
    public void getServiceReportData(String serviceContract, String maxLimit, String offset){
        try{
			sUrl = ApexPages.currentPage().getURL();
			sUrl = EncodingUtil.urlDecode(sUrl.substringAfter('?'), 'UTF-8');
			Blob data = EncodingUtil.base64Decode(sUrl);
			string sParameter = data.toString();
			List<String> lstParams = new List<String>();
			lstParams = sParameter.split('&');
			if(lstParams.size()>0){
				serviceContract = lstParams[0].substringAfter('=');
				maxLimit = lstParams[1].substringAfter('=');
				offset = lstParams[2].substringAfter('=');
			}
            if(String.isBlank(maxLimit)){
                maxLimit = '80';
            }
            if(String.isBlank(offset)){
                offset = '0';
            }
            
			HttpRequest req = new HttpRequest();
			String token = CCP_InvoiceUtility.GetOAuth('CCP_ServiceReport_API');
			Integration_General_Settings__c serviceReportSettings;
			serviceReportSettings = Integration_General_Settings__c.getValues('CCP_ServiceReport_API');
			boolean integEnabled;
			String endPoint;
			if(serviceReportSettings != null){
				integEnabled = serviceReportSettings.Enable_Integration__c;
				endPoint = serviceReportSettings.Endpoint__c;
			}
			if(!String.ISBLANK(serviceContract)){
                
				endPoint = endPoint + '?SR_NBR=' + serviceContract + '&ROWS_OFFST=' + offset + '&MAX_ROWS_LIMT=' + maxLimit;
				req.setEndpoint(endPoint);
				req.setMethod('GET');
				req.setTimeOut(120000);            
				String authorizationHeader = 'Bearer ' +token;
				req.setHeader('Authorization', authorizationHeader);
				req.setHeader('content-type', 'application/json');
				
                Http http = new Http();
				HttpResponse res = new HttpResponse();
                res = Http.send(req);
                String json = res.getBody().replaceAll('Services.','');
                
                Integer statusCode = res.getStatusCode();
                if (statusCode == 200 && res != null && !string.isBlank(json)) {
           			ServiceReportWrap = CCP_ServiceReportWrapper.parse(json);
                    system.debug('ServiceReportWrap ++++ '+ServiceReportWrap);
                }
			}
		}catch(Exception e){
			System.debug(e.getMessage());
		}
    }  
}
Wrapper Class
public class ServiceReportWrapper {
	public ServiceContract_SAM_APIResponse ServiceContract_SAM_APIResponse;

	public class ServiceContract_SAM_APIResponse {
		public ServiceContract_SAM_APIResult ServiceContract_SAM_APIResult;
	}
    
    public class ServiceContract_SAM_APIResult {
		public List<Row> row;
	}

	public class Row {
		public String SR_NBR;
		public String SR_CREATED;
		public String SR_TYPE;
		public String IB_SERIAL;
		public String LABOR_DT;
		public String LABOR_START_TM;
		public String LABOR_END_TM;
		public String TRAVEL_DT;
		public String TRAVEL_START_TM;
		public String TRAVEL_END_TM;
	}

	public static CCP_ServiceReportWrapper parse(String json) {
		return (CCP_ServiceReportWrapper) System.JSON.deserialize(json, CCP_ServiceReportWrapper.class);
	}
}
JSON Response
{
    "ServiceContract_SAM_APIResponse": {
        "ServiceContract_SAM_APIResult": {
            "row": [
                {
                    "SR_NBR": "1-233391976021",
                    "SR_CREATED": "[]",
                    "SR_TYPE": "[]",
                    "IB_SERIAL": "[]",
                    "LABOR_DT": "[]",
                    "LABOR_START_TM": "[]",
                    "LABOR_END_TM": "[]",
                    "TRAVEL_DT": "[]",
                    "TRAVEL_START_TM": "[]",
                    "TRAVEL_END_TM": "[]",
                },
                {
                    "SR_NBR": "1-233391976021",
                    "SR_CREATED": "[]",
                    "SR_TYPE": "[]",
                    "IB_SERIAL": "[]",
                    "LABOR_DT": "[]",
                    "LABOR_START_TM": "[]",
                    "LABOR_END_TM": "[]",
                    "TRAVEL_DT": "[]",
                    "TRAVEL_START_TM": "[]",
                    "TRAVEL_END_TM": "[]",
                },
                {
                    "SR_NBR": "1-233391976021",
                    "SR_CREATED": "2018-04-26T13:44:25.000Z",
                    "SR_TYPE": "Corrective Repair",
                    "IB_SERIAL": "[]",
                    "LABOR_DT": "[]",
                    "LABOR_START_TM": "[]",
                    "LABOR_END_TM": "[]",
                    "TRAVEL_DT": "2018-05-02",
                    "TRAVEL_START_TM": "10:30:00",
                    "TRAVEL_END_TM": "11:30:00",
                },
                {
                    "SR_NBR": "1-233391976021",
                    "SR_CREATED": "2018-04-26T13:44:25.000Z",
                    "SR_TYPE": "Corrective Repair",
                    "IB_SERIAL": "[]",
                    "LABOR_DT": "2018-05-02",
                    "LABOR_START_TM": "11:30:00",
                    "LABOR_END_TM": "14:30:00",
                    "TRAVEL_DT": "[]",
                    "TRAVEL_START_TM": "[]",
                    "TRAVEL_END_TM": "[]",
                }
            ]
        }
    }
}

VF Page
<apex:page controller="ServiceReportCallout" renderAs="PDF" showHeader="false" sidebar="false" standardStylesheets="true" applyBodyTag="false" applyHtmlTag="false">
    <html>           
        <head>
            <apex:stylesheet value="{! URLFOR($Resource.CCP_Resources, '/assets/stylesheets/inspira.css') }"/>
            <apex:stylesheet value="{! URLFOR($Resource.CCP_Resources, '/assets/stylesheets/styles.css') }"/>
            <style type="text/css" media="print">
                @page {
                    /* Page spaces */
                    size: landscape;
                    margin: 0.3cm;
                    margin-bottom: 100px;
                    margin-top: 50px;
                }
                th{
                    scope:col;
                    vertical-align:top;
                    text-align:right;
                }
                td{
                    scope:row;
                    vertical-align:top;
                }
                tr{
                    height:40px;
                }
                tr.border_bottom th{
                      border-bottom: 1pt solid black;
                    text-align: center;
                }
                tr.border_bottom td{
                    text-align: center;
                    border-bottom: 1pt solid black;
                }
            </style>                     
        </head>
        <body>
            <apex:pageBlock>
                <apex:pageBlockTable value="{!ServiceReportWrap}" var="serviceReport">
            <div style="padding-top:10px;">
                <table style="padding:8px;" cellpadding="auto" width="100%" border="0">
                    <colgroup>
                        <col style="width: 15%" />
                        <col style="width: 23%" />
                        <col style="width: 17%" />
                        <col style="width: 23%" />
                        <col style="width: 10%" />
                        <col style="width: 12%" />
                    </colgroup>
                    <tr>
                        <th scope="col">{!$Label.CCP_SR}</th>
                        <td><apex:outputText escape="false" value="&nbsp 1-232858771276"/></td>
                        
                        <th scope="col">{!$Label.CCP_SRType}</th>
                            <td><apex:outputText escape="false" value="&nbsp {!serviceReportWrap.SR_TYPE}"/>
                        </td>
                        
                        <th scope="col">{!$Label.CCP_Serial_No}</th>
                        <td><apex:outputText escape="false" value=""/></td>
                    </tr>       
                </table>
            </div>
                </apex:pageBlockTable>
            <div style="padding-top:10px; page-break-before:always;">
                <br/><br/><br/>
                <apex:outputText style="font-weight: bold;" value="Labor :"/>
                <table>
                    <tr class="border_bottom">
                        <th>{!$Label.CCP_Date}</th>
                        <th>{!$Label.CCP_StartTime}</th>
                        <th>{!$Label.CCP_EndTime}</th>
                    </tr>
                    <apex:repeat value="{!ServiceReportWrap}" var="string" id="laborRepet">
                    <tr class="border_bottom">
                        <td><apex:outputText value="04/16/2018"/></td>
                        <td><apex:outputText escape="false" value="04/16/2018<br>15:26:00"/></td>
                        <td><apex:outputText escape="false" value="04/16/2018<br>15:49:00"/></td>
                      </tr>
                    </apex:repeat>
                </table>
                <br/><br/><br/><br/><br/><br/><br/><br/>
                <apex:outputText style="font-weight: bold;" value="Travel :"/>
                <table>
                    <tr class="border_bottom">
                        <th>{!$Label.CCP_Date}</th>
                        <th>{!$Label.CCP_StartTime}</th>
                        <th>{!$Label.CCP_EndTime}</th>
                    </tr>
                    <apex:repeat value="{!ServiceReportWrap}" var="string" id="travelRepet">
                    <tr class="border_bottom">
                        <td><apex:outputText value="04/16/2018"/></td>
                        <td><apex:outputText escape="false" value="04/16/2018<br>15:26:00"/></td>
                        <td><apex:outputText escape="false" value="04/16/2018<br>15:49:00"/></td>
                      </tr>
                    </apex:repeat>
                </table>
            </div>
            </apex:pageBlock>>
       </body>
    </html>
</apex:page>

Any Idea??
Thanks