1

I have written Apex code and VF page for uploading records for my custom object. While uploading the CSV file into system via programatically I am getting the error "Visualforce Error System.Type Exception: Invalid decimal".

Please refer the screenshot for more details. Usually this error is not coming but sometimes it comes. It throws an error in the particular line of (Line no-411) my Apex code " site.Estimated_Value__c = decimal.valueOf(inputsitevalues[9]);".

I have copied the records into other CSV file and then uploaded it works fine. I am not sure whether it is the File format issue or coding issue. Please help me out in this.

Visualforce Error screenshot

2
  • 1
    My suggestion would be to run the import with debug logs running and see which line of your csv causes the problem; debugging the value for inputsitevalues[9] will help in solving the problem. Something else to try would be to create a method to try and parse the values and throw an exception if an invalid value is found. That way a meaningful error could be shown to the user and detail which line and value is causing the problem. Commented Jul 4, 2013 at 9:10
  • @RichardDurrant Thank you so much for the reply. I will try write a method to throw an exception if an invalid value is found. One more question, Can we use trim() to resolve this issue.
    – Arun
    Commented Jul 4, 2013 at 10:04

4 Answers 4

5

A common problem would be leading white space, try using trim()

site.Estimated_Value__c = decimal.valueOf(inputsitevalues[9].trim());
1
  • Thanks for the reply. I have used trim(), This is not working for passing null value scenario. If the Estimated value data comes null in spreadsheet then it shows the same error. So I have given one more condition to allow the null value if(inputsitevalues[9] != '') { site.Estimated_Value__c = decimal.valueOf(inputsitevalues[9].trim()); } Now the Invalid decimal error is not coming
    – Arun
    Commented Jul 5, 2013 at 4:32
3

I have encountered this problem and founded that when the number column is the last one in the Excel sheet, the system (salesforce) will throw an error: "invalid integer\decimal". Maybe its because it also reads the and of the line tag from the excel, and this is why it has a problem with identifying it as a integer\decimal. Maybe im wrong with the explanation but for sure do not put the number data in the last column when trying to read a csv file in salesforce.

0

Invalid decimal means returning 'N/A', try to use tirnary operator to check if the string value is blank than cast in decimal.

site.Estimated_Value__c = (inputsitevalues[9]=='')?0 : decimal.valueOf(inputsitevalues[9];

0

I agree with Richard's comment on the question. You should try the following to quickly identify the problem.

  1. do a try catch for exception and null and on the catch, print the error record.

  2. Check the excel column and do a right click on the column properties to see if the excel is messing up the data.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .