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
Madhusudan Singh 15Madhusudan Singh 15 

Create Map of Map

Hi All I am a newbie in SFDC and Apex. I am struggling with a silly thing from last night so seeking your help. 
I already gone through multiple Links of Maps and List but it is all giving just a basic Idea but not to what I am expecting
I want to create an data structure like below. I used PHP code to simplify the understanding

array(
    array(
    'CustomerID' => '12345',
    'CustomerName' = > 'Jhon',
    'CustomerRevenue' => 30000,
    ),
    array(
    'CustomerID' => '78927',
    'CustomerName' = > 'Martin',
    'CustomerRevenue' => 200000,
    ),
);

Question1: 
How to transform same values in Apex

Question2: 
How to retrieve Customer Revenue when list is finalized

Question3: How to loop over this to identify each record

Question4:
Now let say if I have another record for existing customer I want 'CustomerRevenue' to be SumUp


For Example another values that I need to add in existing array is Like this

array(
    'CustomerID' => '12345',
    'CustomerName' = > 'Jhon',
    'CustomerRevenue' => 50000,
),
    
Now my new array should be like

array(
    array(
    'CustomerID' => '12345',
    'CustomerName' = > 'Jhon',
    'CustomerRevenue' => 80000, // This value is changed
    ),
    array(
    'CustomerID' => '78927',
    'CustomerName' = > 'Martin',
    'CustomerRevenue' => 200000,
    ),
);

Regards
Madhusudan Singh
Narender Singh(Nads)Narender Singh(Nads)
Hi Madhusudan,
The code will look something like this:
//Data Structure
list< list< map<string, string> > > ls= new list< list< map<string, string> > >();

//Inserting Values in structure

list< map<string, string> > Lmp=new list< map<string, string> >();
map<string, string> m=new map<string, string>();

m.put('CustomerID','12345');
m.put('CustomerName' , 'Jhon',);
m.put( 'CustomerRevenue', '80000'); //Please note that the value is passed as a string not integer
                                    //Because map is of type (string,string);

lmp.add(m);
ls.add(lmp);
system.debug(ls);


//Traversing through through the structure

for( list< map<string,string> > l1 : ls){
    for( map<string,string> mp: l1 ){
		for(string s: mp.keyset()){
           //system.debug(m.get(s));
        }
    }
}

//Getting a particular value

for( list< map<string,string> > l1 : ls){
    for( map<string,string> mp: l1 ){
        //system.debug(mp.get('CustomerRevenue'));
    }
}

But I would sugggest passing such data structure as a JSON object. It is much more easier more to work with a JSON object.

Let me know if it helps.
Thanks!