Retrieve Data Using Apex Code with Viewer

Overview of Apex Code in Viewer

Apex code is a proprietary programming language used in Salesforce's programming platform. While Viewer offers several tools to customize and extend its functionalities, Apex code is particularly powerful for manipulating data and retrieving information from salesforce and also from external systems.

With Apex code, you can perform complex operations on salesforce data, as well as integrate with external systems to fetch and update information. This flexibility makes Apex code a valuable tool for developers looking to tailor the Viewer experience to their specific business needs.

Benefits of Using Apex Code for Data Manipulation and Retrieval

There are several benefits to using Apex code for data manipulation and retrieval in Viewer:

1. Enhanced Customization: Apex code allows developers to build custom functionality, enabling them to tailor the application to their specific businesses requirements.

2. Seamless Integration: By leveraging Apex code, it becomes possible to seamlessly integrate Viewer with external systems. This enables the application to retrieve data from multiple sources, providing users with a comprehensive view of their information without needing to save the data in Salesforce.

Understanding the Syntax and Structure of Apex Code

To write effective Apex code in Viewer, it is crucial to have a good understanding of its syntax and structure. Salesforce Apex provides a useful feature called the Callable Interface. This interface enables developers to define custom code that can be invoked externally from other Salesforce components. 

How to Write Apex Code in Viewer

Writing Apex code in Viewer involves a few simple steps:

1. Create a New Apex Class: Make sure that the class implements Callable interface .

2. Write Your Code: Once you have created the Apex class, start writing your code. follow the example class provided in the article. 

3. Update "External Data Class Name" in your template with the API name of the Class. now you can use the data your class provide in Viewer template to see the date simply click the "Get Data" in the template section and the json in the modal will now contain your data.

4. Test and Deploy to Production : After writing your tests and verify its functionality. Fix any bugs or errors before deploying the code to your production environment.

Example Apex Code

You are expected to return a valid json as the return value from the execution.

You are expected to return a valid json as the return value from the execution.

global with sharing class GetDataPluginExt implements Callable {

  public Object call(String action, Map<String, Object> args) {

    Map<String, Object> input = (Map<String, Object>)args.get('input');
    Map<String, Object> output = (Map<String, Object>)args.get('output');
    Map<String, Object> options = (Map<String, Object>)args.get('options');

    return invokeMethod(action, input, output, options);
  }
  private Object invokeMethod(String methodName, Map<String, Object> inputMap,
                              Map<String, Object> outMap,
                              Map<String, Object> options) {
    switch
      on methodName {
        when 'getData' {
          /// replace the code below 
          String randomNumber =
              String.valueOf(Integer.valueof((Math.random() * 30)));
          String APIUrl = 'https://dummyjson.com/products/' + randomNumber;

          HttpRequest req = new HttpRequest();
          req.setEndpoint(APIUrl);
          req.setMethod('GET');
          req.setHeader('Content-Type', 'application/json');
          req.setHeader('Accept', 'application/json');
          req.setTimeout(120000);
          HTTPResponse res = new Http().send(req);
          // System.debug(res.getBody());

          Map<String, Object> ObjectsList =
              (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
          if (null == ObjectsList) {
            return null;
          }
          return ObjectsList;
         /// replace the code above 
        }
        when else {
          return '';
        }
      }
  }
}

Best Practices for Using Apex Code in Viewer

  1. Write your code according to salesforce best practice
  2. Remember to optimize your cade, as this code run in the context of the Viewer package and have the same limitation as any other transaction limits
  3. Consider making the code lean as posible in order to save heap size to minimal

Was this article helpful?

How to use template filter
Creating Documents in Salesforce using Apex
Feedback