0

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 .

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 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);
        }  
  }
}
17
  • Here type is child of Account? Commented Oct 18, 2016 at 10:28
  • yes type is a child of account Commented Oct 18, 2016 at 10:59
  • check my answer if it works for you. Commented Oct 18, 2016 at 11:05
  • check your updated code. Commented Oct 18, 2016 at 13:52
  • @tushar... i am crying now.. again error..Compile Error: Incompatible value type Boolean for Map<Id,Set<String>> at line 65 column 17 Commented Oct 18, 2016 at 13:55

1 Answer 1

0

You need another map and a loop to complete this step

public map<string, Account> accmap = new map<string, Account>();
List<Type__c> typeList = new List<Type__c>();
for(string str : csvmap.keyset())
{
    Account accObj = new Account() ;
    accObj.name = str ;             
    accObj.accountnumber = csvmap.get(str);                                                                      
    accmap.put(str , accObj); 
}

insert accmap.values();

//second loop
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[2]))
            {
                Type__c t = new Type__c (Name = csvRecordData[2], AccountId__c = acc.id); 
                typeList.add(t);
                typeAccMap.put(csvRecordData[0], typeAccMap.get(csvRecordData[0]).add(csvRecordData[2]));
            }
        }
        else
        {
              Type__c t = new Type__c (Name = csvRecordData[3], AccountId__c = acc.id); 
              typeList.add(t);
              typeAccMap.put(csvRecordData[0], new Set<String>{csvRecordData[2]});
        }

       }
insert typeList;

PS: I am not sure about the csv column number so you can handle that also if some column contains blank you need to handle that as well.

Remove the ID column from csv.

11
  • Hi tushar , i am getting error .. Method does not exist or incorrect signature: [Map<String,Account>].add(String, Account) at line 38 column 12 Commented Oct 18, 2016 at 11:05
  • @learnjqueery you need to do accmap.put(str , accObj); check my answer. Commented Oct 18, 2016 at 11:09
  • tushar.. one record is coming as Account Number : Customer - Channel ,Customer - Channel.. this should be Account Number Customer - Channel . and in this Type__c is haing two records Customer - Channel (2 times) which should be 1 time.. Commented Oct 18, 2016 at 11:16
  • @learnjqueery can you please specify CSV column order csvRecordData[0])+','+csvRecordData[3] because 0 index for ID and we can't specify Id in insert call. Commented Oct 18, 2016 at 11:24
  • The above pic is from the csv file.. Its in that order.. Commented Oct 18, 2016 at 11:36

You must log in to answer this question.

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