0

I have a weired requirement now. In the below pic column A, B, C needs to go to object Account and column D should go to another object Type__c and Type__c should be a child of Account.

Requirement is

Current code is working

If Id's are same then only one record is getting created in Account object and AccountNumber is getting inserted as CC978213;CD355119-A.

Where i am stuck is

When Id's are same one record is getting created in Account simulataneoulsy 1 record should get created in Type__c object if Type is having same value for that ID.And if column A, B , C are same but D is different, one record should get created for Account having one account number and 2 record should get crated in Type__c object .

enter image description here

public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public List<account> acclist{get;set;}
    public map<string, string> csvmap = new map<string, string>();
  public importDataFromCSVController(){
    csvFileLines = new String[]{};
    acclist = New List<Account>(); 
  }

  public void importCSVFile(){
       try{
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n'); 


      for(Integer i=1;i<csvFileLines.size();i++){

           string[] csvRecordData = csvFileLines[i].split(',');
                if(csvmap.containsKey(csvRecordData[0])){
           csvmap.put(csvRecordData[0],csvmap.get(csvRecordData[0])+','+csvRecordData[3]);
               }
                else{
                csvmap.put(csvRecordData[0],csvRecordData[3]) ;
                    }
            }

           system.debug('@@@@@@'+ csvmap);
    for(string str : csvmap.keyset())
     {
       Account accObj = new Account() ;

           accObj.name = str ;             
           accObj.accountnumber = csvmap.get(str);                                                                      
           acclist.add(accObj); 

      }   

        insert acclist;
        }
        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);
        }  
  }
}
  • How are you deciding that record is duplicated? – Tushar Sharma Oct 17 '16 at 18:04
  • Currently for ex you can take id or name as duplicate record...Just for example ...i can modify it later.. – learnjqueery Oct 17 '16 at 18:05
  • Why you removed your code sample? – Tushar Sharma Oct 18 '16 at 6:44
  • I am going to update the updated one.. – learnjqueery Oct 18 '16 at 8:57
0

You can easily do this using map

Create a map and fill it and remove duplicate

map<string, string> csvmap = new map<string, string>();
for(Integer i=1;i<csvFileLines.size();i++){

           string[] csvRecordData = csvFileLines[i].split(',');
           if(csvmap.containsKey(csvRecordData[0]))
           svmap.put(csvRecordData[0],csvmap.get(csvRecordData[0])+','+csvRecordData[1])
           else
                svmap.put(csvRecordData[0],csvRecordData[1])                                                                       
   }

then iterate over this map

for(string str : svmap.keyset())
     {
       Account accObj = new Account() ;

           accObj.name = str ;             
           accObj.accountnumber = svmap.get(str);                                                                       
           acclist.add(accObj); 

      } 
      insert acclist; 

PS: few things this is a case sensitive match if you want a case insensitive match then you can convert them into lower case

| improve this answer | |
  • One small question.. Where we are inserting the account number with comma separated.. like test__c = CC978213, CD355119-A... – learnjqueery Oct 17 '16 at 18:17
  • @learnjqueery svmap.get(str) in this map value part we are storing account number in comma separated format. Then we will fill this in account number field. – Tushar Sharma Oct 17 '16 at 18:19
  • if i want to insert accountnumber do i need to write as - for(string str : csvmap.keyset()) { Account accObj = new Account() ; accObj.name = str ; accObj.accountnumber = str; accObj.test__c = csvmap.get(str); acclist.add(accObj); } – learnjqueery Oct 17 '16 at 18:57
  • You should probably use String.join and a Map<Id, Set<String>> instead. – Adrian Larson Oct 17 '16 at 19:09
  • 1
    sure i will post in new question – learnjqueery Oct 18 '16 at 10:24

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.