function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sumit dsumit d 

Creating map two ids as key in map

 hi All,
i Created a map in which i am taking two ids as key .i created the map as below:-
 Map<Id,FBG_Family__c> mapAlreadyexitFamily = new Map<Id,FBG_Family__c>();
        for(FBG_Family__c existFamily : [select id, Name, Sister_Company__c, Sister_Company2__c FROM FBG_Family__c ]){
            String key = String.valueOf(existFamily.Sister_Company__c) + String.valueOf(existFamily.Sister_Company__c);
            mapAlreadyexitFamily.put(key,existFamily);
        }
but its giving me error:- Invalid id: nullnull.
how can i create a map having two ids as a key and get value related to that key which is  a record.
Any suggestions, how to do it?
Best Answer chosen by sumit d
Avishek Nanda 14Avishek Nanda 14
Hi Sumit,

Check the containsKey in Map. That should work. 
if(!mapAlreadyexitFamily.containKey(Family)){
                ListFBGFamily.add(Family); 
            }

Regards,

Avishek 

 

All Answers

Avishek Nanda 14Avishek Nanda 14
Hi Sumit,

You have taken Map parameter as id and sObject however adding a Srting which is why you are getting error invalid id. 
 
Map<String,FBG_Family__c> mapAlreadyexitFamily = new Map<String,FBG_Family__c>();
for(FBG_Family__c existFamily : [select id, Name, Sister_Company__c, Sister_Company2__c FROM FBG_Family__c ]){
    String key = String.valueOf(existFamily.Sister_Company__c) + String.valueOf(existFamily.Sister_Company__c);
    mapAlreadyexitFamily.put(key,existFamily);
    system.debug(mapAlreadyexitFamily.values());
}


Change the Parameter you should be able to add the values to Map. 

Regards,
Avishek 
sumit dsumit d
 Hi avishek,
     i want to create record when the record is not present in the map so i tried like below:-
       //creating junction object records
        List<FBG_Family__c> ListFBGFamily = new List<FBG_Family__c>();
        for (Account acc : accounts)
        {
            List<Account> DuplicateAccount = mapDuplicateAccNameToIds.get(acc.Name);
            System.debug('DuplicateAccount'+DuplicateAccount);
            for(Account Acc1 : DuplicateAccount ){
                 FBG_Family__c Family =  new FBG_Family__c();
                Family.Sister_Company__c =  acc.id;
                if( acc.Id != Acc1.Id){
                    Family.Sister_Company2__c = Acc1.id;
                    String key = ''+  Family.Sister_Company__c + Family.Sister_Company2__c;
                    if(Family != mapAlreadyexitFamily.get(key)){
                        ListFBGFamily.add(Family); 
                    }
                } 
            }
        }
        but still its inserting records  which r already present in the map. 
        how  to solve this any suggestions?
Avishek Nanda 14Avishek Nanda 14
Hi Sumit,

Check the containsKey in Map. That should work. 
if(!mapAlreadyexitFamily.containKey(Family)){
                ListFBGFamily.add(Family); 
            }

Regards,

Avishek 

 

This was selected as the best answer