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
Veerendar AellaVeerendar Aella 

how to import JSON file into lead records from apex and visualforce page by using single click import button

Hi All,

I want to import Json file into my leads record using apex and visualforce page.
Please help.
 
Best Answer chosen by Veerendar Aella
Raj VakatiRaj Vakati
Try this
 
public class Parser_New{
    public Blob myfile{get;set;}
    public Parser_New(){
        reports = new fromJSON(); 
    }
    
    public fromJSON reports {get;set;}
    
    public void doUpload() {
        System.debug('myfile'+myfile.toString());
        reports =  (fromJSON) System.JSON.deserialize(myfile.toString(), fromJSON.class);
		
		
		List<cls_record> rec = reports.leaddata.record ; 
		
		
		List<Lead> leadList = new List<Lead>();
		for(cls_record c :rec){
			leadList.add( new Lead( FirstName =c.FirstName ,LastName = c.LastName , Phone =c.Phone , Email =c.Email) ;
		}
		insert leadList;
        
    }
    
    public class fromJSON{
        public cls_leaddata leaddata{get;set;}
    }
    public class cls_leaddata {
        public cls_record[] record{get;set;}
    }
    public  class cls_record {
        public String FirstName{get;set;}	//Veera
        public String LastName{get;set;}	//Vinnu
        public String Phone{get;set;}	//8008997654
        public String Email{get;set;}	//veerav@gmail.com
    }
    
    
    
    
}

 

All Answers

Raj VakatiRaj Vakati
Try this code


JSON file 
 
{
	"leaddata": {
		"record": [
			{
				"FirstName": "Veera",
				"LastName": "Vinnu",
				"Phone": "8008997654",
				"Email": "veerav@gmail.com"
			},
			{
				"FirstName": "Beera",
				"LastName": "Binnu",
				"Phone": "9898989898",
				"Email": "beeru@gmail.com"
			},
			{
				"FirstName": "Ceera",
				"LastName": "Cinnu",
				"Phone": "8989898989",
				"Email": "ceera@gmail.com"
			},
			{
				"FirstName": "Deera",
				"LastName": "Dinnu",
				"Phone": "7878787878",
				"Email": "deera@gmail.com"
			}
		]

	}
}



 
public class Parser_New{
    public Blob myfile{get;set;}
    public Parser_New(){
        reports = new fromJSON(); 
    }
    
    public fromJSON reports {get;set;}
    
    public void doUpload() {
        System.debug('myfile'+myfile.toString());
        reports =  (fromJSON) System.JSON.deserialize(myfile.toString(), fromJSON.class);
        
    }
    
    public class fromJSON{
        public cls_leaddata leaddata{get;set;}
    }
    public class cls_leaddata {
        public cls_record[] record{get;set;}
    }
    public  class cls_record {
        public String FirstName{get;set;}	//Veera
        public String LastName{get;set;}	//Vinnu
        public String Phone{get;set;}	//8008997654
        public String Email{get;set;}	//veerav@gmail.com
    }
    
    
    
    
}

Page
 
<apex:page Controller="Parser_New" >
    
    <apex:form >        
        <apex:pageblock title="Read XML" id="PB">
            <!-- inputFile for uploading XML -->
            <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 XML Result -->
            <apex:pageblocksection title="Result of XML" columns="1">
                <apex:pageblocktable value="{!reports.leaddata.record}" var="con">
                    <apex:column value="{!con.FirstName}" headerValue="First Name"/>
                    
                </apex:pageblocktable>
            </apex:pageblocksection>
            <!-- Button for calling method of controller -->
            <center>
                <apex:commandButton value="Read" action="{!doUpload}"/>
            </center>
        </apex:pageblock>    
    </apex:form>    
    
</apex:page>

 
Raj VakatiRaj Vakati
DOes its solved?
Veerendar AellaVeerendar Aella
Yes raj, thanks alot.
Veerendar AellaVeerendar Aella
Hi Raj,
Can you please let me know how to use insert in the above to import the data into the lead records?
Raj VakatiRaj Vakati
Try this
 
public class Parser_New{
    public Blob myfile{get;set;}
    public Parser_New(){
        reports = new fromJSON(); 
    }
    
    public fromJSON reports {get;set;}
    
    public void doUpload() {
        System.debug('myfile'+myfile.toString());
        reports =  (fromJSON) System.JSON.deserialize(myfile.toString(), fromJSON.class);
		
		
		List<cls_record> rec = reports.leaddata.record ; 
		
		
		List<Lead> leadList = new List<Lead>();
		for(cls_record c :rec){
			leadList.add( new Lead( FirstName =c.FirstName ,LastName = c.LastName , Phone =c.Phone , Email =c.Email) ;
		}
		insert leadList;
        
    }
    
    public class fromJSON{
        public cls_leaddata leaddata{get;set;}
    }
    public class cls_leaddata {
        public cls_record[] record{get;set;}
    }
    public  class cls_record {
        public String FirstName{get;set;}	//Veera
        public String LastName{get;set;}	//Vinnu
        public String Phone{get;set;}	//8008997654
        public String Email{get;set;}	//veerav@gmail.com
    }
    
    
    
    
}

 
This was selected as the best answer
Veerendar AellaVeerendar Aella
Hi Raj, 
superb thanks bro.
sai prakash 7sai prakash 7
hai. i have stored the json file in a static resource and i want to retrieve the data using JSON.deserialize() in apex.how can i do that?