Inserting excel file using apex, I have 4 columns in file,Column-1 (Name) can contain duplicate values. I want to perform check if column-1 has same values (for example A) and column-2(codes) has different values for that particular A-Name want to give error saying that the codes are different for same name.

My approaching is added column1 to the list1 and column4 to the list2 but it doesn't seem to work.

If anyone know the logic kindly help me with this ?

Part of code :

     List<customObject> newList = new List<customObject>();
        String[] errors = new List<String>();
        map<String,String> nameToCode = new map<String,String>();
        for (Integer i=1;i<lines.size();i++){
           String[] valuesFromFile = new String[]{};
           valuesFromFile = lines[i].split(',');

           customObject co = new customObject();
           co.Name__c = valuesFromFile[0];
           co.City__c = valuesFromFile[1];
           co.State__c = valuesFromFile[2];
           co.Code__c = valuesFromFile[3];

            if (nameToCode.containsKey(co.Name__c) {
              if (nameToCode.get(co.Name__c) != co.Code__c)
                 ApexPages.addmessage(new ApexPages.Message(ApexPages.severity.Error,'Please check code for Name:' + co.Name__c));
 else nameToCode.put(co.Name__c,co.Code__c);

         insert newList;
  • Can you add a helper column? Concatenate Name__c and Code__c then you will can compare them together in a single check. – JaredT Jul 12 '16 at 15:24
  • @jatuttle0 Thanks for your response this file will be uploaded by some other users and they cannot concatenate – SFDC Jul 12 '16 at 15:26

You'll need to use a map of nameToCode as in:

String[] errors = new List<String>();
map<String,String> nameToCode = new map<String,String>();
for (Integer i = 1; i < lines.size(); i++) {

   ... parse and add to customObject

   if (nameToCode.containsKey(co.Name__c) {
      if (nameToCode.get(co.Name__c) != co.Code__c)
         errors.add('Row ' + i + ' has different code for Name: ' + co.Name__c + ' as entered elsewhere in CSV.');
      else {}  // benign

Some things to think about:

  1. Your CSV line parser will break if there are commas embedded in a cell's value.
  2. The map solution above is case sensitive. Adjust accordingly to be case insensitive if that is a requirement.
  • thanks fro your response sorry if you misunderstood my question. Codes are like (1,2,3,4) and Names will be like {A,B,C,D} I want to ensure Name A -> has code 1(in file) then all the codes for Name A should be 1 if not give error – SFDC Jul 12 '16 at 16:59
  • @SFDC - that is what the code does, it tracks in a map all Names and the first code for that Name encountered, then the next time the same name is found in the file, it compares the current code to the saved (first-encountered) code – cropredy Jul 12 '16 at 17:01
  • I added the logic you provided, it is showing error for all the records even if they have same codes :( – SFDC Jul 12 '16 at 17:47
  • @SFDC - use the Edit link and amend your OP to reflect current code – cropredy Jul 12 '16 at 18:05
  • edited the code & added nameToCode.put(co.Name__c,co.Code__c); first ,if not it will be always false and allowing all records – SFDC Jul 12 '16 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.