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
Itayb34Itayb34 

Error on Insert records with CSV file

Hello

 
I'm using a code I found to insert record to SF via csv file (here is the blog I used the general template: http://www.forcetree.com/2010/08/read-and-insert-records-from-csv-file.html)
 
I've create a custom object so that users will be able to import log records using excel. I've encountered an issue: all the fields that I created are Number data type and in the original file, the data types are String.
I'm getting an error message:  "Error: Compile Error: Illegal assignment from String to Decimal at line 19 column 13"
 
public class FileUploader 
{
    public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    String[] filelines = new String[]{};
    List<Log_File__c> logstoupload;
    
    public Pagereference ReadFile()
    {
        nameFile=contentFile.toString();
        filelines = nameFile.split('\n');
        logstoupload = new List<Log_File__c>();
        for (Integer i=1;i<filelines.size();i++)
        {
            String[] inputvalues = new String[]{};
            inputvalues = filelines[i].split(',');
            
            Log_File__c a = new Log_File__c();
            a.Number_Of_Hosts__c = inputvalues[0];
            a.Number_Of_VMs__c = inputvalues[1];       
            a.Number_Of_Centers__c = inputvalues[2];
             logstoupload.add(a);
        }
        try{
        insert logstoupload;
        }
        catch (Exception e)
        {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
            ApexPages.addMessage(errormsg);
        }    
        return null;
    }
    
    public List<Log_File__c> getuploadedAccounts()
    {
        if (logstoupload!= NULL)
            if (logstoupload.size() > 0)
                return logstoupload;
            else
                return null;                    
        else
            return null;
    }            
}
 
 
Can someone can assist in solving this? this tool is something that I need desperately...(users need to upload files and they can't use the starndard import custom object funcionality - a big permission issue)
Also, does anyone knows how to start writing a test class for this?
 
Thanks in Advance!
 
Itay
Taiki YoshikawaTaiki Yoshikawa

Hi,

 

I think need to be converted.

 

a.Number_Of_Hosts__c = Decimal.valueOf(inputvalues[0]);

or

a.Number_Of_Hosts__c = Integer.valueOf(inputvalues[0]);

 

 

Itayb34Itayb34

This is what I did, thanks!

 

I have another problem: when importing the template, i'm getting the following error:

BLOB is not a valid UTF-8 string

Error is in expression '{!ReadFile}' in component <apex:page> in page upload_logs
 

my visualforce page is as follows:
<apex:page sidebar="false" StandardController="Log_File__c" recordSetVar="Log Files" extensions="FileUploader">
   <apex:form >
      <apex:sectionHeader title="Upload data from CSV file"/>
      <apex:pagemessages />
      <apex:pageBlock >
             <center>
              <apex:inputFile value="{!contentFile}" filename="{!nameFile}" /> <apex:commandButton action="{!ReadFile}" value="Upload File" id="Import_Log_File" style="width:70px;"/>
              <br/> <br/> <font color="red"> <b>Note: Please use the standard template to upload Accounts. <a href="{!URLFOR($Resource.LogUploadTemplate)}" target="_blank"> Click here </a> to download the template. </b> </font>
             </center>  
            
      <apex:pageblocktable value="{!uploadedLogs}" var="log" rendered="{!NOT(ISNULL(uploadedLogs))}">
          <apex:column headerValue="Number Of VMs">
              <apex:outputField value="{!log.Number_Of_VMs__c}"/>
          </apex:column>
          <apex:column headerValue="Number Of Hosts">
              <apex:outputField value="{!log.Number_Of_Hosts__c}"/>
          </apex:column>
      </apex:pageblocktable> 
      
      </apex:pageBlock>       
   </apex:form>  
</apex:page>

 

 
I know that I need to translate the csv into UTF-8, but how? any help will be appriciated...
 
Itayb34Itayb34

now i'm getting: 

Invalid integer: 35
Error is in expression '{!ReadFile}' in component <apex:page> in page upload_logs


One of the values in the excel is 35... Any help? apex class is as follows:
public class FileUploader 
{

    public FileUploader(ApexPages.StandardSetController controller) {

    }

    public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    String[] filelines = new String[]{};
    List<Log_File__c> logstoupload;
    
    public Pagereference ReadFile()
    {
        nameFile=contentFile.toString();
        filelines = nameFile.split('\n');
        logstoupload = new List<Log_File__c>();
        for (Integer i=1;i<filelines.size();i++)
        {
            String[] inputvalues = new String[]{};
            inputvalues = filelines[i].split(',');
             
            Log_File__c a = new Log_File__c();
            a.Number_Of_Hosts__c = Integer.valueOf(inputvalues[0]);
            a.Number_Of_VMs__c = Integer.valueOf(inputvalues[1]);       
            a.Number_Of_Centers__c = Integer.valueOf(inputvalues[2]);
            logstoupload.add(a);
        }
        try{
        insert logstoupload;
        }
        catch (Exception e)
        {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
            ApexPages.addMessage(errormsg);
        }    
        return null;
    }
    
    public List<Log_File__c> getuploadedLogs()
    {
        if (logstoupload!= NULL)
            if (logstoupload.size() > 0)
                return logstoupload;
            else
                return null;                    
        else
            return null;
    }            
}

 

 
Itayb34Itayb34

Almost there...I'm trying to redirect the page (after inserting the CSV) to a case: I've created a lookup between the custom object "Log File" and Case, and in the class i've added the row

a.Case__c = ApexPages.currentPage().getParameters().get('id'); to capture the case ID and link the logs to the case when uploading. Is it the correct way to do so??

 

The only thing left for me is redirecting to the case record... any help?

NoamNoam
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 sign 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.
Naveen KNNaveen KN
Check the below link
https://www.codengine.in/2019/06/blob-is-not-a-valid-utf-8-string-salesforce-apex.html