I have a weird 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 .
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 map<string, Account> accmap = new map<string, Account>();
List<Type__c> typeList = new List<Type__c>();
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[2]);
}
else{
csvmap.put(csvRecordData[0],csvRecordData[2]) ;
}
}
system.debug('@@@@@@'+ csvmap);
/* for(string str : csvmap.keyset())
{
Account accObj = new Account() ;
accObj.name = str ;
accObj.test__c= csvmap.get(str);
acclist.add(accObj);
}
insert acclist;*/
for(string str : csvmap.keyset())
{
Account accObj = new Account() ;
accObj.name = str ;
accObj.test__c= csvmap.get(str);
accmap.put(str , accObj);
}
insert accmap.values();
Map<Id, set<string>> typeAccMap = new Map<Id, set<string>>();
for(Integer i=1;i<csvFileLines.size();i++){
string[] csvRecordData = csvFileLines[i].split(',');
if(accmap.containsKey(csvRecordData[0])){
Account acc = accmap.get(csvRecordData[0]);
if(typeAccMap.containsKey(acc.id))
{
Set<String> typeSet = typeAccMap.get(acc.id);
if(! typeSet.contains(csvRecordData[3]))
system.debug('@@@@@' + typeSet);
system.debug('@@@@@' + csvRecordData[3]);
{
Type__c t = new Type__c (Name = csvRecordData[3], Account__c = acc.id);
typeList.add(t);
// typeAccMap.put(csvRecordData[0], typeAccMap.get(csvRecordData[0]).add(csvRecordData[3]));
Set<string> tmpSet = typeAccMap.get(csvRecordData[0]);
tmpSet.add(Id.valueOf(csvRecordData[3]));
typeAccMap.put(csvRecordData[0], tmpSet);
}
}
else
{
Type__c t = new Type__c (Name = csvRecordData[3], Account__c = acc.id);
typeList.add(t);
typeAccMap.put(csvRecordData[0], new Set<String>{csvRecordData[3]});
}
}
upsert typeList;
}
}
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);
}
}
}