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
Øyvind Borgersen 10Øyvind Borgersen 10 

apex csv file upload with duplicate check


I've created a code for uploading a csv file. But I need to enhance this code with a duplicate check to prevent a record in the csv file is uploaded if the related case has been uploaded already this quarter.

From the code below, I've added a query to to check if record's registration date is in within current quarter. If so, then the record should be excluded from the upload. However, this does not work correctly as it does not exclude all of the records which has already been uploaded this quarter. Does anyone has a good idea of why it's failing and how to correct it? Or any other way I could try this code?

public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public List<Videogram_sales_number__c> vidList{get;set;}
//Public List<String> showList{get;set;}
public importDataFromCSVController(){
csvFileLines = new String[]{};
vidList = New List<Videogram_sales_number__c>();
public void importCSVFile(){
csvAsString = csvFileBody.toString();
csvFileLines = csvAsString.split('\n');
Date lastPerEnd = [Select EndDate From Period Where type = 'Quarter' and StartDate = THIS_FISCAL_QUARTER].EndDate;
Date lastPerStart = [Select StartDate From Period Where type = 'Quarter' and StartDate = THIS_FISCAL_QUARTER].StartDate;

List<Videogram_sales_number__c> vidListDat = [SELECT id,
FROM Videogram_sales_number__c
WHERE Registration_date__c <:lastPerEnd
AND Registration_date__c >:lastPerStart];
system.debug('vidListDat' + vidListDat);
for(Integer i=1;i<csvFileLines.size();i++){
Videogram_sales_number__c vidObj = new Videogram_sales_number__c();
//Create date as string
String dateFormatString = 'yyyy-MM-dd';
Date d =;
Datetime dt = Datetime.newInstance(d.year(), d.month(),;
String dateString = dt.format(dateFormatString);
//Split by comma or semicolon
string[] csvRecordData = csvFileLines[i].split(',|\\;');
if(!(csvRecordData[3] =='') && !string.valueof(vidListDat).contains(string.valueof(csvRecordData[0]))){
//&& !string.valueof(vidListDat).contains(string.valueof(csvRecordData[0]))
vidObj.Case_ID__c = csvRecordData[0];
vidObj.RegistrationNumberDummy__c = csvRecordData[1];
//vidObj.RegistrationNumberDummy__c = dateString;
vidObj.Titledummy__c = csvRecordData[2];
vidObj.Number_of_copies__c = decimal.valueof(csvRecordData[3].trim());
vidObj.Registration_date__c =;
insert vidList;
system.debug('vidlist' + vidlist);
ApexPages.Message successMessage = new ApexPages.Message(ApexPages.severity.CONFIRM,'Fil er korrekt lastet opp // File successfully uploaded');
else {
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'En feil har oppstått, vennligst sjekk at filen er korrekt og at salgstall ikke allerede er rapportert i dette kvartal // An error has occured while importing data Please make sure input csv file is correct nad that salesnumber not already is uploaded in this quarter');
catch (Exception e)
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importing data Please make sure input csv file is correct');