Author - StudySection Post Views - 5,762 views
JSON

How to Import data from a JSON file into multiple Objects in Salesforce using Visualforce Page

It is a common practice in salesforce to upload records from a JSON file into our orgs. In this post, we are going to learn how to view and insert the records from a single JSON file to more than one object, be it a Standard Object or a Custom one. This code will display the records on the Visualforce Page and import them into the Lead and Account Vehicles Objects.

To upload our sample JSON file, we’ll need a Visualforce Page. The controller for this Visualforce page will be an Apex Class that will contain the code to get the file and deserialize the JSON from it and then insert the records into the required Objects.

Let’s begin with creating our Apex Class:

//Class Named JSONUploader that will be used as a controller for our Visualforce Page
public class JSONUploader{
//myfile will get our file from the Visualforce Page
public Blob myfile{get;set;}
//Constructor of this class that will be used to initialize the object, here “reports” will store contents of the JSON file
public fromJSON reports {get;set;}
public JSONUploader(){
reports = new fromJSON();
}

//Getting the main header from our JSON file, here leaddata is the header of our JSON file
public class fromJSON{
public cls_leaddata leaddata{get;set;}
}

//Inside the header, “record1” contains the records that we will be storing inside our first object and “record2” contains the records that we will store inside our second object.
public class cls_leaddata {
public cls_record1[] record{get;set;}
public cls_record2[] record2{get;set;}
}

//Getting the field names and their values
public class cls_record1 {
public String FirstName{get;set;}
public String LastName{get;set;}
public String Phone{get;set;}
public String Email{get;set;}
public String Company{get;set;}
}
public class cls_record2 {
public String VName{get;set;} //Veera
public String AName{get;set;} //Vinnu
public String PNum{get;set;} //8008997654
}

//This method will fetch the records from the JSON file and put them in a list and then insert them into our Objects, here we are inserting the data in our Standard Object Lead and Custom Object named Account Vehicles
public void doUpload() {
System.debug('myfile'+myfile.toString());

//This will deserialize the JSON
reports = (fromJSON) System.JSON.deserialize(myfile.toString(), fromJSON.class);
List rec = reports.leaddata.record;
List rec2 = reports.leaddata.record2;
List leadList = new List();
List Alist = new List();
for(cls_record c :rec){
leadList.add( new Lead( FirstName =c.FirstName ,LastName = c.LastName , Phone =c.Phone , Email =c.Email, Company =c.Company)) ;
}
for(cls_record2 c2 :rec2){
Alist.add( new Account_Vehicles__c( Account_Name__c =c2.AName ,Name = c2.VName , Policy_Number__c =c2.PNum)) ;
}
insert leadList;
insert Alist;
}
}

Now let’s create our Visualforce Page :
<apex:page Controller="Parser_New" >
<apex:form >
<apex:pageblock title="Read JSON" id="PB">
<!-- inputFile for uploading JSON -->
<apex:pageblocksection >
<apex:pageblocksectionitem >
<apex:outputLabel value="Please Select JSON File:"/>
<apex:inputFile value="{!myfile}"> </apex:inputFile>
</apex:pageblocksectionitem>
</apex:pageblocksection>
<apex:page Controller="Parser_New" >
<apex:form >
<apex:pageblock title="Read JSON" id="PB">
<!-- inputFile for uploading JSON -->
<apex:pageblocksection >
<apex:pageblocksectionitem >
<apex:outputLabel value="Please Select JSON File:"/>
<apex:inputFile value="{!myfile}"> </apex:inputFile>
</apex:pageblocksectionitem>
</apex:pageblocksection>

<!-- Table to show the JSON Result -->
<apex:pageblocksection title="Result of JSON" columns="1">
<apex:pageblocktable value="{!reports.leaddata.record}" var="con">
<apex:column value="{!con.FirstName}" headerValue="First Name"/>
<apex:column value="{!con.Phone}" headerValue="Phone"/>
<apex:column value="{!con.Email}" headerValue="Email"/>
</apex:pageblocktable>

<apex:pageblocktable value="{!reports.leaddata.record2}" var="con2">
<apex:column value="{!con2.VName}" headerValue="Vehicle Name"/>
<apex:column value="{!con2.AName}" headerValue="Account Name"/>
<apex:column value="{!con2.PNum}" headerValue="Policy Number"/>
</apex:pageblocktable>
</apex:pageblocksection>

<!-- Button for calling method of controller -->
<center>
<apex:commandButton value="Read" action="{!doUpload}"/>
</center>
</apex:pageblock>
</apex:form>
</apex:page>

Sample JSON File:
{
"leaddata": {
"record": [
{
"FirstName": "Shivam",
"LastName": "Rana",
"Phone": "8608997654",
"Email": "sr@gmail.com",
"Company" : "C5"

},
{
"FirstName": "Ashish",
"LastName": "Kumar",
"Phone": "9348989898",
"Email": "ak@gmail.com",
"Company" : "C6"
},
{
"FirstName": "Manisha",
"LastName": "Sharma",
"Phone": "8559898989",
"Email": "ms@gmail.com",
"Company" : "C7"
},
{
"FirstName": "Harpreet",
"LastName": "Singh",
"Phone": "7988787878",
"Email": "hs@gmail.com",
"Company" : "C8"
}
],

"record2": [
{
"VName": "Vijay",
"AName": "Walia",
"PNum": "8008997651"
},
{
"VName": "Jai",
"AName": "Kumar",
"PNum": "8008997652"
},
{
"VName": "Karan",
"AName": "Rana",
"PNum": "8008997653"
},
{
"VName": "Ankush",
"AName": "Rajput",
"PNum": "8008997654"
}
]

}
}

The English language is the most widely used medium of communication around the world. Having a certification for the English language can be an advantage. StudySection provides an English Certification Exam that tests English language proficiency in English grammar, reading, and writing.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri