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

Need test class for apex class import xml into salesforce org
Hi All,
I have an apex class for importing xml data into salesforce org, I need a test class code to test the data. Please help.
Below is my apex class:
public class ImportAccountsXML {
public Blob myfile{get;set;}
public ImportAccountsXML(){
reports = new List<Account>();
}
public List<Account> reports {get;set;}
public class Accdata {
public String Name {get; set;}
public String AccountNumber {get; set;}
public String Phone {get; set;}
public String Industry {get; set;}
public String type {get; set;}
public String Description {get; set;}
public String Site {get; set;}
public String Ownership {get; set;}
public String Sic {get; set;}
public String Website {get; set;}
}
private void parseReports(DOM.XMLNode node) {
for (Dom.XMLNode child : node.getChildElements()) {
if (child.getName() == 'record') {
System.debug('child'+child);
parseReport(child);
// reports.add(r);
}
System.debug('reports'+reports);
}
}
private void parseReport(DOM.XMLNode node ) {
account r = new account();
for (Dom.XMLNode child : node.getChildElements()) {
if (child.getName() == 'Name') {
r.Name= child.getText().trim();
} else if (child.getName() == 'AccountNumber') {
r.AccountNumber= child.getText().trim();
} else if (child.getName() == 'Phone') {
r.Phone= child.getText().trim();
} else if (child.getName() == 'Industry') {
r.Industry= child.getText().trim();
} else if (child.getName() == 'Type') {
r.Type= child.getText().trim();
}else if (child.getName() == 'Ownership') {
r.Ownership= child.getText().trim();
}else if (child.getName() == 'Sic') {
r.Sic= child.getText().trim();
}else if (child.getName() == 'Description') {
r.Description= child.getText().trim();
}else if (child.getName() == 'Website') {
r.Website= child.getText().trim();
}else if (child.getName() == 'Site') {
r.Site= child.getText().trim();
}
}
reports.add(r);
upsert reports;
}
public void doUpload() {
DOM.Document doc = new DOM.Document();
doc.load(String.valueOf(myfile.toString()));
parseReports(doc.getRootElement());
}
}
I have an apex class for importing xml data into salesforce org, I need a test class code to test the data. Please help.
Below is my apex class:
public class ImportAccountsXML {
public Blob myfile{get;set;}
public ImportAccountsXML(){
reports = new List<Account>();
}
public List<Account> reports {get;set;}
public class Accdata {
public String Name {get; set;}
public String AccountNumber {get; set;}
public String Phone {get; set;}
public String Industry {get; set;}
public String type {get; set;}
public String Description {get; set;}
public String Site {get; set;}
public String Ownership {get; set;}
public String Sic {get; set;}
public String Website {get; set;}
}
private void parseReports(DOM.XMLNode node) {
for (Dom.XMLNode child : node.getChildElements()) {
if (child.getName() == 'record') {
System.debug('child'+child);
parseReport(child);
// reports.add(r);
}
System.debug('reports'+reports);
}
}
private void parseReport(DOM.XMLNode node ) {
account r = new account();
for (Dom.XMLNode child : node.getChildElements()) {
if (child.getName() == 'Name') {
r.Name= child.getText().trim();
} else if (child.getName() == 'AccountNumber') {
r.AccountNumber= child.getText().trim();
} else if (child.getName() == 'Phone') {
r.Phone= child.getText().trim();
} else if (child.getName() == 'Industry') {
r.Industry= child.getText().trim();
} else if (child.getName() == 'Type') {
r.Type= child.getText().trim();
}else if (child.getName() == 'Ownership') {
r.Ownership= child.getText().trim();
}else if (child.getName() == 'Sic') {
r.Sic= child.getText().trim();
}else if (child.getName() == 'Description') {
r.Description= child.getText().trim();
}else if (child.getName() == 'Website') {
r.Website= child.getText().trim();
}else if (child.getName() == 'Site') {
r.Site= child.getText().trim();
}
}
reports.add(r);
upsert reports;
}
public void doUpload() {
DOM.Document doc = new DOM.Document();
doc.load(String.valueOf(myfile.toString()));
parseReports(doc.getRootElement());
}
}
All Answers
I am getting below errors:
Please find code and xml format below:
Code:
public class Parser_New{
public Blob myfile{get;set;}
public Parser_New(){
reports = new List<leaddata>();
}
public List<leaddata> reports {get;set;}
public class leaddata {
public String FirstName {get; set;}
public String LastName {get; set;}
public String Phone {get; set;}
public String Email {get; set;}
}
private void parseReports(DOM.XMLNode node) {
for (Dom.XMLNode child : node.getChildElements()) {
if (child.getName() == 'record') {
System.debug('child'+child);
parseReport(child);
// reports.add(r);
}
System.debug('reports'+reports);
}
}
private void parseReport(DOM.XMLNode node ) {
leaddata r = new leaddata();
for (Dom.XMLNode child : node.getChildElements()) {
if (child.getName() == 'FirstName') {
r.FirstName= child.getText().trim();
} else if (child.getName() == 'LastName') {
r.Lastname= child.getText().trim();
} else if (child.getName() == 'Phone') {
r.Phone= child.getText().trim();
} else if (child.getName() == 'Email') {
r.Email= child.getText().trim();
}
}
reports.add(r);
}
public void doUpload() {
DOM.Document doc = new DOM.Document();
doc.load(String.valueOf(myfile.toString()));
parseReports(doc.getRootElement());
}
}
XML Format:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<lead-data >
<record>
<FirstName>Veera</FirstName>
<LastName>Vinnu</LastName>
<Phone>8008997654</Phone>
<Email>veerav@gmail.com</Email>
</record>
<record>
<FirstName>Beera</FirstName>
<LastName>Binnu</LastName>
<Phone>9898989898</Phone>
<Email>beeru@gmail.com</Email>
</record>
<record>
<FirstName>Ceera</FirstName>
<LastName>Cinnu</LastName>
<Phone>8989898989</Phone>
<Email>ceera@gmail.com</Email>
</record>
<record>
<FirstName>Deera</FirstName>
<LastName>Dinnu</LastName>
<Phone>7878787878</Phone>
<Email>deera@gmail.com</Email>
</record>
</lead-data>
Awesome..! it worked fine, Thanks.