0

enter image description here

In the below pic column[0], column[1], column[2] gets inserted in Account object and column[3] gets inserted in Type__c object.

Taking reference column[0] I am checking duplicate.

For the fist two records I am able to merge column[0], column[1], column[2] as one record and insert it in Account object.

But I am unable to merge column[3] to one record and insert it to 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]) ;
                }
            }

            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]))
                        {   
                            Type__c t = new Type__c (Name = csvRecordData[3], Account__c = acc.id); 
                            typeList.add(t);

                            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);
        }  
    }
}

For first 2 rows the data which is getting inserted is as below

Account Name - GenePoint Test
Account Number - CC978213,CD355119-A

Child Object - Type__c should get inserted as below -

Only one record
1. Name - Customer - Channel

Instead of 
1. Name - Customer - Channel

2. Name - Customer - Channel
  • Is there some code missing? There's no reference to a Type__c object anywhere in your code. – Nick Cook Oct 19 '16 at 2:26
  • Hi Nick.. sorry for the confusion.. I hv updated my code. – learnjqueery Oct 19 '16 at 2:29
  • When you say "I am unable to merge column[3] to one record and insert it to Type__c object", do you get an error? What happens? What do you want to happen? You really need to be more clear with your questions. – Nick Cook Oct 19 '16 at 2:47
  • instead of inserting - Customer - Channel - two times.. i want to insert it for 1 time in Types__c.. Type__c is a child of Account object.. – learnjqueery Oct 19 '16 at 2:49
  • But aren't there 3 different accounts there? In which case, you'd need 3 different Types? – Nick Cook Oct 19 '16 at 3:16
0
    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]) ;
                    }
                }

                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<String, Set<String>> accountIDtoTypes = new Map<String, Set<String>>();

                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]) && 
(!accountIDtoTypes.containskey(csvRecordData[0]) || (accountIDtoTypes.containskey(csvRecordData[0]) &&
!accountIDtoTypes.get(svRecordData[0]).contains(svRecordData[3]))))
                    {
                        if(accountIDtoTypes.containsKey(csvRecordData[0]))
                        {
                            accountIDtoTypes.get(csvRecordData[0]).add(csvRecordData[3]);
                        } else {
                            accountIDtoTypes.put(csvRecordData[0],new Set<String>{csvRecordData[3]});
                        }
                        Account acc = accmap.get(csvRecordData[0]);

                        if(typeAccMap.containsKey(acc.id))
                        {
                            Set<String> typeSet = typeAccMap.get(acc.id);

                            if(! typeSet.contains(csvRecordData[3]))
                            {   
                                Type__c t = new Type__c (Name = csvRecordData[3], Account__c = acc.id); 
                                typeList.add(t);

                                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);
            }  
        }
    }
  • Hi Andrei...Customer - Direct...the last row should get inserted in Type__c..but its not happening...duplicate check for first two rows is working correctly...can u plz check.. – learnjqueery Oct 19 '16 at 4:15
  • I can not get 100% what your requirements are. This code will insert 1 Type__c per AccountID. see image s18.postimg.org/8qecoyuyx/what2.jpg – Andrei.Z Oct 19 '16 at 4:33
  • ok from ur pic...forth row should get inserted as a child to 001..So 001 will have child object as 2 records TypeA, TypeB...Same for 002.. – learnjqueery Oct 19 '16 at 4:42
  • do you also need to insert row 3 from my example? as it is TypeB – Andrei.Z Oct 19 '16 at 4:45
  • 1
    Changed to insert all unique child. I did not touched rest of your code but it needs to be refactored as well in my opinion. – Andrei.Z Oct 19 '16 at 5:18

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.