You need to sign in to do that
Don't have an account?
Veerendar Aella
Illegal assignment from String to Decimal, Please help.
Hi All,
Below is my Code for Importing XML into Salesforce org.
Apex Code:
public class ImportOppXML {
public Blob myfile{get;set;}
public ImportOppXML(){
reports = new List<Opportunity>();
}
public List<Opportunity> reports {get;set;}
public class Oppdata {
public String Name {get; set;}
public Decimal Amount {get; set;}
public Date CloseDate {get; set;}
public String Description {get; set;}
public String LeadSource {get; set;}
public String NextStep {get; set;}
public String StageName {get; set;}
public String Type {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 ) {
opportunity r = new opportunity();
for (Dom.XMLNode child : node.getChildElements()) {
if (child.getName() == 'Name') {
r.Name= child.getText().trim();
} else if (child.getName() == 'Amount') {
r.Amount= child.gettext().trim();
} else if (child.getName() == 'CloseDate') {
r.CloseDate= child.getText().trim();
} else if (child.getName() == 'Description') {
r.Description= child.getText().trim();
} else if (child.getName() == 'LeadSource') {
r.LeadSource= child.getText().trim();
}else if (child.getName() == 'NextStep') {
r.NextStep= child.getText().trim();
}else if (child.getName() == 'StageName') {
r.StageName= child.getText().trim();
}else if (child.getName() == 'Type') {
r.Type= 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());
}
}
Below is my Code for Importing XML into Salesforce org.
Apex Code:
public class ImportOppXML {
public Blob myfile{get;set;}
public ImportOppXML(){
reports = new List<Opportunity>();
}
public List<Opportunity> reports {get;set;}
public class Oppdata {
public String Name {get; set;}
public Decimal Amount {get; set;}
public Date CloseDate {get; set;}
public String Description {get; set;}
public String LeadSource {get; set;}
public String NextStep {get; set;}
public String StageName {get; set;}
public String Type {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 ) {
opportunity r = new opportunity();
for (Dom.XMLNode child : node.getChildElements()) {
if (child.getName() == 'Name') {
r.Name= child.getText().trim();
} else if (child.getName() == 'Amount') {
r.Amount= child.gettext().trim();
} else if (child.getName() == 'CloseDate') {
r.CloseDate= child.getText().trim();
} else if (child.getName() == 'Description') {
r.Description= child.getText().trim();
} else if (child.getName() == 'LeadSource') {
r.LeadSource= child.getText().trim();
}else if (child.getName() == 'NextStep') {
r.NextStep= child.getText().trim();
}else if (child.getName() == 'StageName') {
r.StageName= child.getText().trim();
}else if (child.getName() == 'Type') {
r.Type= 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());
}
}
} else if (child.getName() == 'Amount') {
r.Amount= decimal.valueof(child.gettext().trim());
} else if (child.getName() == 'CloseDate') {
r.CloseDate= Date.valueof(child.getText().trim());
Specifically for the decimal issue: For the potential date problem, you'll need to convert the string into a date. If the format returned is mm/dd/yyyy you can use date.parse(dateString)
I have used
elseif (child.getName() == 'CloseDate') {
r.CloseDate= Date.parse(child.getText().trim());
But I am getting the error
Visualforce Error
Help for this Page
System.XmlException: Failed to parse XML due to: expected name start and not < (position: TEXT seen ...<CloseDate>2018/12/11</<... @6:33)
Error is in expression '{!doUpload}' in component <apex:commandButton> in page importoppxml: Class.ImportOppXML.doUpload: line 124, column 1
So it looks like your string is 2018/12/11 (yyyy/mm/dd format). date.parse would be looking for mm/dd/yyyy and date.valueOf(strDate) is looking for a yyyy-mm-dd format. I think the easiest way to fix your problem would be to convert your '/' into '-' and use the Date.valueOf() method.
Let me know if you're still running into any errors and I'll try to help.
-Arri