I am inserting records using CSV file. While adding file without any deletion its working perfectly fine.

In my code i am checking if the value is equal to null giving error. So when I delete values from the csv file (deleting values not rows) It is still assuming it has values and giving error "Error uploading" (added by me in logic).

How to overcome this issue ?

Class :

     for (Integer i = 1; i < filelines.size(); i++) {
 String[] inputvaluesFile = new String[] {};
 inputvaluesFile = filelines[i].split(',');

 productList__c prod = new productList__c();
 if (inputvaluesFile[0] != '') {
  prod.Number__c = inputvaluesFile[0];
 prod.Note__c = inputvaluesFile[1];
 prod.Description__c = inputvaluesFile[2];
 if (inputvaluesFile[3] != '') {
  prod.unit__c = decimal.valueOf(inputvaluesFile[3]);

 if (inputvaluesFile[4] != '') {
  prod.Price__c = decimal.valueOf(inputvaluesFile[4]);

 // if i delete some values in csv its still assuming it has record instead of treating as end of file  and giving below error

    if (prod.Number__c != null && prod.Note__c != null && prod.AccountId != null && prod.Price__c != null) {

else  ApexPages.addmessage(new ApexPages.Message(ApexPages.severity.Error, 'Error uploading the product'));

2 Answers 2


You should probably check and make sure the row isn't empty first. I'm actually kind of surprised this doesn't crash with a ListException. You can just change the loop near the top:

String[] inputvaluesFile = filelines[i].split(',');
if(inputvaluesFile.size() == 1) {

You should also generally guard against invalid CSV files, like if someone forgets the last column.


When you create your new productlist (productList__c prod = new productList__c();), This creates a new object with all null fields.

So if you don't fill some of those fields because they are empty in the CSV, then they will still be null in your final check. Which will trigger the error.

So rather than triggering the error, if certain key values are missing, just don't add the new prod to the list of products that you are going to insert:

if (prod.Number__c != null && prod.Note__c != null && prod.AccountId != null && prod.Price__c != null) {

Another option is to use the Database.insert method with allOrNone=false, like so:

// perform insert
Database.SaveResult[] srList = Database.insert(insertList, false);

// Iterate through each returned result
for (Database.SaveResult sr : srList) {
    if (!sr.isSuccess()) {
        for(Database.Error err : sr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());

For more information, look here

  • Thanks for your response I am trying to do a soft insert so even if some records are failed it should insert the rest except failures ones. So for that I need to check null values right..
    – Anjali
    Commented Jul 11, 2016 at 17:09
  • Well in that case, don't even bother to check for any missing data and just to a Database.insert(insertList,false); <- false meaning allOrNothing = false, which will allow all valid records to be inserted, even if there are errors. Commented Jul 11, 2016 at 17:12
  • Thank you ! With this is it possible to show which records are failed ?
    – Anjali
    Commented Jul 11, 2016 at 17:22
  • Yes, updated my answer to show you. Commented Jul 11, 2016 at 17:27
  • thanks for your response I used 1st option (not null) but still same result :( am i missing anything
    – Anjali
    Commented Jul 14, 2016 at 2:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .