Create JSON file in apex and store in Static Resource

8:53 PM

To create a JSON file in Apex and store it as a static resource, you can follow these steps:

Prerequisite: 

MetadataService should be installed from here. Just hit the 'Deploy to Salesforce' button to install in your salesforce instance.

  1. Create a JSON object or an object hierarchy that you want to serialize to a JSON file. In this example we are creating a JSON file with 4 fields queried from the Product2 object

  2. Use the JSON.serialize() method to serialize the JSON object to a string.

  3. Create a new StaticResource object with the Name and Body fields set to the desired values. The Body field should be set to a Blob value of the JSON string.

  4. Create the StaticResource using the MetadataService createMetadata method.

Here's an example of how to create a JSON file and store it as a static resource:

 

0 comments