I have a requirement to read a csv file and insert tasks into database. Trying with Apex because we need to associate them to Contacts but we don't have Contact Id instead we have Contact Email and other details. So need to search Contact for each row and associate a task to it. I am still in the beginning so trying to insert Tasks by populating all mandatory fields in the file. But getting an error called 'Invalid Id' while setting WhoId(Contact Id) on a task. If I remove that line, it's inserting fine. Am not sure if I am missing a key thing here or if we can't associate task to a Contact pro-grammatically. (the line in Bold is causing error)
public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public List<Task> Tasklist{get;set;}
public importDataFromCSVController(){
csvFileLines = new String[]{};
Tasklist = New List<Task>();
}
public void importCSVFile(){
try{
csvAsString = csvFileBody.toString();
csvFileLines = csvAsString.split('\n');
for(Integer i=1;i<csvFileLines.size();i++)
{
Task taskObj = new Task();
string[] csvRecordData = csvFileLines[i].split(',');
taskObj.OwnerId = csvRecordData[0];
taskObj.Subject = csvRecordData[1];
taskObj.Status = csvRecordData[2];
taskObj.Priority = csvRecordData[3];
taskObj.WhatId = csvRecordData[4];
**taskObj.WhoId = csvRecordData[5];**
Tasklist.add(taskObj);
}
Insert Tasklist;
}
catch (Exception e)
{
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
ApexPages.addMessage(errorMessage);
}
}
}
Error in Debug Log:: |VARIABLE_SCOPE_BEGIN|[31]|e|Exception|true|false 16:43:43.0 (17110101) |VARIABLE_ASSIGNMENT|[31]|e|"common.apex.runtime.impl.ExecutionException: Invalid id: 003g000001VvWkR\r"|0x6add22b6 16:43:43.0 (17117180) |STATEMENT_EXECUTE|[31]