0

Tring to convert List <MBT_SFFB__Facebook_Page__c> to Map<String,MBT_SFFB__Facebook_Page__c>

These are the fields in Object :- MBT_SFFB__Facebook_Page__c

Select  ID,MBT_SFFB__Facebook_ID__c,MBT_SFFB__Facebook_Page_ID__c,MBT_SFFB__Facebook_Page_Name__c,MBT_SFFB__Facebook_User_Relates_To_Page__c,MBT_SFFB__Image__c from MBT_SFFB__Facebook_Page__c

Map Manipulation

map<String,MBT_SFFB__Facebook_Page__c> objmapfacebookallpages = new map<String,MBT_SFFB__Facebook_Page__c>();
list<MBT_SFFB__Facebook_Page__c> objlstalldbMBT_SFFB_Facebook_Page =new list<MBT_SFFB__Facebook_Page__c>([Select  ID,MBT_SFFB__Facebook_ID__c,MBT_SFFB__Facebook_Page_ID__c,MBT_SFFB__Facebook_Page_Name__c,MBT_SFFB__Facebook_User_Relates_To_Page__c,MBT_SFFB__Image__c from MBT_SFFB__Facebook_Page__c]);
       if(objlstalldbMBT_SFFB_Facebook_Page.size()>0)
       {
            for(MBT_SFFB__Facebook_Page__c objfbpguser : objlstalldbMBT_SFFB_Facebook_Page)         
            {
                objmapfacebookallpages.put(objfbpguser.MBT_SFFB__Facebook_Page_ID__c,objfbpguser);                    
            }

        }
System.Debug(objmapfacebookallpages);

Output:- {11470233432895363662=MBT_SFFB__Facebook_Page__c:{MBT_SFFB__Facebook_ID__c=11470233432895363662, MBT_SFFB__Facebook_Page_ID__c=16162323367178658991, MBT_SFFB__Facebook_Page_Name__c=Busyday, MBT_SFFB__Facebook_User_Relates_To_Page__c=a0H2800000B6nR0EAJ, MBT_SFFB__Image__c=https://scontent.xx.fbcdn.net/v/t1.0-1/c15.0.50.50/p50x50/399548_10149999285987789_1102888142_n.png?oh=82d551fbd5c70ae9e2ad123aaebbc331&oe=58E6646A, Id=a0I2800000EhUi2EAF}}

The only last Record i found.

I want all records.

  • There are few things you can check on. First check the size of your list objlstalldbMBT_SFFB_Facebook_Page. Then check whether MBT_SFFB__Facebook_Page_ID__c field value is unique over all the records – highfive Dec 19 '16 at 5:42
  • What's the use of creating a List and then converting it to Map? You can directly have a for loop iteration on MBT_SFFB__Facebook_Page__c and create a Map – RCS Dec 19 '16 at 5:44
2

Please find the below optimized code. Please run the soql in the developer console/workbench to find how many records are there in MBT_SFFB__Facebook_Page__c object.

Map<String, MBT_SFFB__Facebook_Page__c> objmapfacebookallpages = new Map<String, MBT_SFFB__Facebook_Page__c>();

for(MBT_SFFB__Facebook_Page__c obj : [Select  ID, MBT_SFFB__Facebook_ID__c, MBT_SFFB__Facebook_Page_ID__c, MBT_SFFB__Facebook_Page_Name__c, MBT_SFFB__Facebook_User_Relates_To_Page__c, MBT_SFFB__Image__c from MBT_SFFB__Facebook_Page__c]) {
    objmapfacebookallpages.put(obj.MBT_SFFB__Facebook_Page_ID__c,obj);
}

system.debug(objmapfacebookallpages);
0

One liner

Map<Id, MBT_SFFB__Facebook_Page__c> objmapfacebookallpages = new Map<Id, MBT_SFFB__Facebook_Page__c>([Select  ID, MBT_SFFB__Facebook_ID__c, MBT_SFFB__Facebook_Page_ID__c, MBT_SFFB__Facebook_Page_Name__c, MBT_SFFB__Facebook_User_Relates_To_Page__c, MBT_SFFB__Image__c from MBT_SFFB__Facebook_Page__c]);
system.debug(objmapfacebookallpages);

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.