I am working with the new JSON Parsing functionality, and I have an API URL that contains a JSON string. I have created one Visualforce Page that displays JSON string data in an apex:pageblockTable in record format from that API URL.

Now I want to insert that JSON string into custom object records. How can I insert new records from JSON string to my custom object?

  • 2
    Can you provide your code ? – SF_user Oct 9 '14 at 11:52
  • 1
    What you have tried so far? – SFIntegrator Oct 9 '14 at 12:58

Please have a look at this post http://sudipta-deb.blogspot.in/2014/10/json-parsing-with-apex-in-salesforce_17.html

The above link will tell you how to parse and display JSON Data into Tabular format. Now in order to store the same into sObject use below functionality-

public class CountryController {
private List<CountryDataWrapper> countryDataWrapper;

public void parseJSONData(){
    countryDataWrapper = new ParseMultipleJsonData().parse();

public List<CountryDataWrapper> getCountries(){
    return countryDataWrapper;

private void insertCountryInformation(){
    List<Country__c> allCountries = new List<Country__c>();
    for(CountryDataWrapper aCountryDataWrapper : countryDataWrapper){

        allCountries.add(new Country__c(
            Name = aCountryDataWrapper.countryName,
            Capital__c = aCountryDataWrapper.countryCapital,
            Currency__c = aCountryDataWrapper.countryCurrency

You can do this very easily using <apex:remoteObjects>.

First you will want to define the fields on the object you care about. Here's how you do it for the Contact object.

<apex:remoteObjects >
    <apex:remoteObjectModel name="Contact" jsShorthand="cont" fields="Name,Id,FirstName,LastName,Phone,Email,Title,AccountId">

This can appear anywhere within your <apex:page> tag.

Next you'll want to write the Javascript to parse the JSON and insert the new record.

//Create the JS object from the JSON
var contactFromJSON = JSON.parse('{!YOUR_JSON_HERE}');

//Function to Create Contact record
function Createcontact(contact){
    var createCon = new SObjectModel.cont({
        FirstName: contact.FirstName,
        LastName: contact.LastName,
        Title: contact.Title,
        Phone: contact.Phone,
        Email: contact.Email});

Then the code to insert the new Contact will just pass the Object which is taken from the JSON as an argument to the function like this: createContact(contactFromJSON);

Now you've created a new Contact. Hope that answers your question. For more information about remoteObjects, see Building Single Page Apps with JavaScript and Visualforce Remote Objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.