function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sayyada HabibSayyada Habib 

Generating QR Code using Apex

Hi, 

I am trying to generate a QR code for a customer based on their License Plate number using an Apex class on a VisualForce page.

I get the following error when I preview my VisualForce page: 

Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2] 
An unexpected error has occurred. Your solution provider has been notified. (System)

My code is as follows:

public with sharing class Customer{

public String qrcode {get;set;}

    public Customer(ApexPages.StandardController stdController) {
        Customer__c customer = (Customer__c)stdController.getRecord();
        List <Customer__c> c = [SELECT Id, License_Plate__c FROM Customer__c WHERE Id =:customer.Id];
        String customerLicensePlate = customer.License_Plate__c;
        
        String apikey = '96b8470ea48cf0f6551db96f3e505005268b52d1';
        
        String requestEndpoint = 'https://api.scanova.io';
        requestEndpoint += '?q=' + customerLicensePlate;
        requestEndpoint += '&APPID=' +apiKey;
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(requestEndpoint);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        
        if(response.getStatusCode() == 200) {
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            Map<String, Object> mainResults = (Map<String, Object>)(results.get('main'));
            qrcode = String.valueOf(mainResults.get('qrcode'));
        
        }
        
    }


}

Any ideas as to why I am getting the error above?

Thanks.
Dushyant SonwarDushyant Sonwar

Hi Sayadda,

The Error means that the api response is not in JSON format. 

To check , comment the code and put the debug to see response you are getting from api

System.debug(response.getBody() + '   response you are getting from api @@@@@@');
/*  
if(response.getStatusCode() == 200) {
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            Map<String, Object> mainResults = (Map<String, Object>)(results.get('main'));
            qrcode = String.valueOf(mainResults.get('qrcode'));
        
        }*/
and copy the response to below URL to validate the JSON.
https://jsonlint.com/ 

Hope this helps.
Sayyada HabibSayyada Habib
Hi Dushyant,

Thank you for your response.

I ran the debug as mentioned above, however when previewing the VisualForce page I just got a blank screen.
Gabriel LinusGabriel Linus
Hello Sayadda

The error message you're encountering indicates an issue with the response you're receiving from the external API. It suggests that the response you're trying to parse as JSON doesn't contain valid JSON data.
Here are some steps to debug and resolve the issue:
  • Check the API Endpoint: Verify that the requestEndpoint you're constructing is correct and points to a valid API endpoint. Make sure there are no typos or missing components in the URL.
  • API Key: Ensure that your API key is valid and has the necessary permissions to access the API.
  • Response Content: Before parsing the response, debug or log the response content to see what you're receiving from the API. You can add the following line just before parsing the response:
    System.debug('Response Content: ' + response.getBody());
           This will help you see if the response contains valid JSON data or if it's something else.
  • Response Error Handling: Implement error handling to deal with cases where the API response is not as expected. For example, you can check the response status code and handle different scenarios accordingly.
    // ... (previous code)

    HttpResponse response = http.send(request);

    if (response.getStatusCode() == 200) {
       String responseBody = response.getBody();
       System.debug('Response Content: ' + responseBody);

       try {
           Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(responseBody);
           Map<String, Object> mainResults = (Map<String, Object>)(results.get('main'));
           qrcode = String.valueOf(mainResults.get('qrcode'));
       } catch (Exception e) {
           // Handle JSON parsing errors or other exceptions
           System.debug('Error parsing JSON: ' + e.getMessage());
       }
   } else {
       // Handle non-200 HTTP status codes
       System.debug('HTTP Request Error: Status Code ' + response.getStatusCode());
       // You can also log the response body here for further analysis
       System.debug('Response Content: ' + response.getBody());
   }
Additionally, for further insights into QR code generation within Salesforce, be sure to explore the article available at https://arrify.com/salesforce-qr-code-generator/. This resource offers a comprehensive guide, outlining the steps to generate QR codes in Salesforce,  along with practical use cases.