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 

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());

         

         

    }

     

}

User-added image
Veerendar AellaVeerendar Aella
Just did minor changes, It worked fine..

} else if (child.getName() == 'Amount') {

                r.Amount= decimal.valueof(child.gettext().trim());
                

            } else if (child.getName() == 'CloseDate') {

                r.CloseDate= Date.valueof(child.getText().trim());
Arrielle KooimanArrielle Kooiman
I think you're going to run into issue with both Amount and Date. When you're parsing your XML it's coming in as a string so you need to convert the string into a decimal and a date. 

Specifically for the decimal issue:
else if (child.getName() == 'Amount') {
                r.Amount= decimal.valueOf(child.gettext().trim());
}
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)


 
Veerendar AellaVeerendar Aella
Hi Kooiman,

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
 
Arrielle KooimanArrielle Kooiman
Hi!

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. 
 
elseif (child.getName() == 'CloseDate') {
                String dateString = child.getText().trim();
                dateString = dateString.replaceAll('/', '-');
                r.CloseDate= Date.valueOf(dateString);
}
Let me know if you're still running into any errors and I'll try to help. 

-Arri