I'm trying to convert a number in a CSV file column into a value that I can save in a custom object field. My custom object has a Geocode field type, which is comprised of two individual fields (Geoloc__Latitude__s and Geoloc__Longitude__s). When I try to convert the String to an Integer, I get the above error.

Public void readCsvFile() {
    if(csvData != null) {
        csvStringFileData = csvData.toString();
        csvFileLines = csvStringFileData.split('\n');

        for(string st:csvFilelines[0].split(',')) 

           for(Integer i=1;i<csvFilelines.size();i++) {
            Client_Site__c clientLocation = new Client_Site__c();
            string[] csvRecordData = csvfilelines[i].split(',');
            clientLocation.SITE_NAME__c = csvRecordData[0];
            clientLocation.ADDRESS__c = csvRecordData[1];
            clientLocation.CITY__c = csvRecordData[2];
            clientLocation.STATE__c = csvRecordData[3];
            clientLocation.ZIP__c = csvRecordData[4];
            clientLocation.FRANCHISEE__c = csvRecordData[5];
            clientLocation.SITE_ID__c = csvRecordData[6];
            clientLocation.SITE_OWNER__c = csvRecordData[7];
            clientLocation.Geoloc__Latitude__s = Integer.valueOf(csvRecordData[8]);
            clientLocation.Geoloc__Longitude__s = Integer.valueOf(csvRecordData[9]);

            clientLocation.RequestForProposal__c = rfpId;


How do I convert the String to something I can save in a Geocode field?


Try changing integer.valueof(csvRecordData[8]); to decimal.valueof(csvRecordData[8]);

  • Nope, I get a similar error with decimal: System.TypeException: Invalid decimal: -91.141739 Error is in expression '{!readCsvFile}' in component <apex:commandButton> in page client_site_upload: Class.ClientSiteUploadController.readCsvFile: line 45, column 1 – Michael Sobczak Apr 10 '15 at 17:51
  • 1
    That's bizarre. Converting to decimal works for me. Have you tried debugging the value in which you're attempting to assign to the latitude? Make sure the value you're attempting to convert is your expected value. – Berg Apr 10 '15 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.