Skip to main content
1 of 3

Not Able to parse date filed from CSV file, It is giving Illegal assignment from String to Date

I am facing issue with importing Date field from CSV file. I am giving the date as mm/dd/yyyy in CSV file, and getting error as "Illegal assignment from String to Date".

Below is my code:

public static void Dataload(List CaseListFromTrigger){
string emailMessageId;
string str; Blob blobbody;
List recordstoupload; List headervalues = new List(); string nameFile; String[] filelines = new String[]{}; string AtachParId; String csvContent; boolean doSentEmail = false;

emailMessageId=[SELECT Id, ParentId FROM EmailMessage where ParentId = :CaseListFromTrigger].Id;
system.debug('***emailMessageId-->'+emailMessageId);

for(Case casedes: [Select Description, Subject from Case where id =: CaseListFromTrigger]){
    str= (casedes.Subject).trim();            
} 

for(Attachment Atch:[SELECT  Id, ParentId,Name,body  FROM Attachment where ParentId = :emailMessageId]){
    blobbody= Atch.Body;
    AtachParId=Atch.ParentId; 
}
system.debug('description--->'+str);                                
System.debug('\n Selected Object--->'+str);

if(str == null)
{
    System.debug('If object is not present');
}
else{
    //Convert the uploaded file which is in BLOB format into a string
     nameFile =blobToString( blobbody,'ISO-8859-1');
     
    //Now sepatate every row of the excel file
    filelines = nameFile.split('\n');
    Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
    Schema.SObjectType objecttypeinfo=gd.get(str);
     
    //Iterate through every line and create a Account record for each row
    recordstoupload= new List<Sobject>();
    headerValues =new List<String>();
    headerValues = filelines[0].trim().split(',');
    System.debug('**headerValues**'+headerValues );
    
    for (Integer i=1;i<filelines.size();i++)
    {
        String[] inputvalues = new String[]{};
        inputvalues = filelines[i].split(',');
        System.debug('**inputvalues**'+inputvalues);
        sObject sObj = Schema.getGlobalDescribe().get(str).newSObject() ;
        for(Integer j=0;j<headerValues.size();j++){
            System.debug('**headerValues[j]**'+headerValues[j]+'***inputvalues[j]**'+inputvalues[j]);
            sObj.put(headerValues[j],String.ValueOf(inputvalues[j])) ;                 
        }
        System.debug('**sObj**'+sObj);
        recordstoupload.add(sObj);
    }

    csvContent = headerValues +'\n';
    Database.SaveResult[] srList = Database.insert(recordstoupload, false);
    for (Database.SaveResult sr : srList) {
        if (sr.isSuccess()) {
        System.debug('**SUCCESS**'+sr.getId());
            csvContent = csvContent+'\n';
        }
        else{
            for(Database.Error err : sr.getErrors()) {
                csvContent = csvContent + sr.getErrors()  + '\n';
                System.debug('If object is not present Exception'+sr.getErrors());
            }
        }                
    }       
}       

}

public static String blobToString(Blob input, String inCharset){ String hex = EncodingUtil.convertToHex(input); System.assertEquals(0, hex.length() & 1); final Integer bytesCount = hex.length() >> 1; String[] bytes = new String[bytesCount]; for(Integer i = 0; i < bytesCount; ++i) bytes[i] = hex.mid(i << 1, 2); return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), inCharset); }

Please let me know if i am missing anything.

enter image description here