You need to sign in to do that
Don't have an account?

need test class for apex class Import Json
Hi All,
I have an Apex class for importing Json file into the salesforce org, I need a test for this apex code. Please help.
Apex Code:
public class ParserJson_New{
public Blob myfile{get;set;}
public ParserJson_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, Company =c.Company, Fax =c.Fax, Description =c.Description
, Industry =c.Industry, Status =c.Status, Website =c.Website, LeadSource =c.LeadSource));
}
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;}
public String LastName{get;set;}
public String Phone{get;set;}
public String Email{get;set;}
public String Company{get;set;}
public String Fax{get;set;}
public String Description{get;set;}
public String Industry{get;set;}
public String Status{get;set;}
public String Website{get;set;}
public String LeadSource{get;set;}
}
}
Json File:
{
"leaddata": {
"record": [
{
"FirstName": "Zepra",
"LastName": "Json",
"Phone": "8008237678",
"Email": "Zepra@gmail.com",
"Company": "Zepra Inc",
"Fax": "143215",
"Industry": "Zepra",
"Description": "This is my Zepra Json",
"Status": "Working -Contacted",
"Website": "WWW.ZepraJson.com",
"LeadSource": "Web"
}
]
}
}
I have an Apex class for importing Json file into the salesforce org, I need a test for this apex code. Please help.
Apex Code:
public class ParserJson_New{
public Blob myfile{get;set;}
public ParserJson_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, Company =c.Company, Fax =c.Fax, Description =c.Description
, Industry =c.Industry, Status =c.Status, Website =c.Website, LeadSource =c.LeadSource));
}
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;}
public String LastName{get;set;}
public String Phone{get;set;}
public String Email{get;set;}
public String Company{get;set;}
public String Fax{get;set;}
public String Description{get;set;}
public String Industry{get;set;}
public String Status{get;set;}
public String Website{get;set;}
public String LeadSource{get;set;}
}
}
Json File:
{
"leaddata": {
"record": [
{
"FirstName": "Zepra",
"LastName": "Json",
"Phone": "8008237678",
"Email": "Zepra@gmail.com",
"Company": "Zepra Inc",
"Fax": "143215",
"Industry": "Zepra",
"Description": "This is my Zepra Json",
"Status": "Working -Contacted",
"Website": "WWW.ZepraJson.com",
"LeadSource": "Web"
}
]
}
}
All Answers