0

I am trying to insert data from csv file to salesforce from vf page using apex. 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.

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

below is the code . I am unable to save it as it is throwing an error

Error: Compile Error: Incompatible value type Boolean for Map<Id,Set<String>> at line 67 column 17

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

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

1 Answer 1

2

In Apex the Set class' add() method returns a Boolean which returns true if the Set changed as a result of the call (see the Documentation). In your code:

typeAccMap.put(csvRecordData[0], typeAccMap.get(csvRecordData[0]).add(csvRecordData[3]));

is trying to put <Id, Boolean> instead of <Id, Set<String>>.

Try replacing line 67 with the following:

Set<Id> tmpSet = typeAccMap.get(csvRecordData[0]);
tmpSet.add(Id.valueOf(csvRecordData[3]));
typeAccMap.put(csvRecordData[0], tmpSet);
12
  • Hi Jeff.. can u let me know which part of my code needs to be modified..How to call typeAccMap.get(csvRecordData[0]).add(csvRecordData[3]) anf then add then add set to the map.. Oct 18, 2016 at 17:48
  • I have edited the end of my answer to hopefully be clearer. Oct 18, 2016 at 18:20
  • another error poped up... Illegal assignment from Set<String> to Set<Id> at line 68 column 24 Oct 18, 2016 at 18:24
  • Should be fixed now. Oct 18, 2016 at 18:26
  • the error is still there....can u plz check.. Oct 18, 2016 at 18:27

You must log in to answer this question.

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