You need to sign in to do that
Don't have an account?
Jameslong
NEED HELP IN UNDERSTANDING A NESTED MAP...PLEASE
Hi,
I am trying to use a nested map to create mapping between IDs of two objects which are product and its related object AMproducts__c.
The business case is each Product will have multiple AMproducts__c linked to it.(1:n relation).
I am trying to use this....
map<id,List<id>>
where id will be product id and List<id> will be a list of its corresponding AMproducts ids.
Please help me with the syntax and how to populate values in the map to create a successful nested map with ids in them.
Thanks
JL
You are getting just a list of ID's for the Parent, and a List of Children with just the name field...
Let me break it down more for you:
All Answers
Why not just use:
Map<id, AMProducts__c[]> or Map<id,List<AMProducts__c>>
So:
Map<id, AMProducts__c[]> mProdAMProd = New Map<id,AMProducts__c[]>();
//Get records using nested select
Product[] prod = [Select ID, (Select Name From ****child API name**** (i.e APProducts__r) from Product Where xxxxxxxx];
//Build Map
For (Product p : prod)
mProdAMProd.put( p.id, p.***child API Name***(i.e. APProducts__r));
// mProdAMProd.put( p.id, p.APProducts__r); as an example
Then you can use the map as needed
Hi Starz26,
Appreciate your time in helping me get started.:smileyhappy:
But, it says compile error while saving the class and the error is
Compile Error: expecting right curly bracket, found 'For'
I fixed it by putting the for loop in {} brackets and it did the job.
However, Before I mark this as a solution...i have a couple of questions
1. can you please confirm that the final map after the for loop contains mapping of product ids and AMproduct ids? In short is the map mProdAMProd ready to use?
2. Is the 1:n relationship between the objects taken care of in the code or some extensions need to be done. If so please suggest.
Thanks a lot
JL
1. mProdAMProd at that point should contain all the ID's for Products in the trigger and for each ID a list of Children Objects. ID => ChildObject[]
2. The relationship was built when you created the child object and select Master Detail. In the code above the second select statement basically pulls the selected field from all the children related to the Main object.
Hi Starz26,
That is clear as water. Thanks a lot. :smileyhappy:
I still have one thing...
we are getting the entire object here :
Map<id, AMProducts__c[]> mProdAMProd = New Map<id,AMProducts__c[]>();
I just need to get the list of IDs only. Is there a way and Can you help with this change please.
Thanks a lot
JL
You are getting just a list of ID's for the Parent, and a List of Children with just the name field...
Let me break it down more for you:
Hi Starz26,
Thanks for the detailed explanation. Now, I understand whats going on clearly.:smileyhappy:
Since we are getting "List of Children with just the name field..." , I simply add ID to the select statement to get the child records Ids as below:
And finally....whats the best way to view the contents of this final map to check if the code is satisfactory or not ?
Thanks a lot
JL
ID is returned regardless of it it is in the select statement or not, but it will not hurt to add it.
2. you can do:
system.debug(mProdAMProd); to view it in the debug log.......
Would you like to display it somewhere else?
Hi Startz26,
I would like to view it in the dubug log only.....
As you suggested I have added the debug statement.
I tried looking into Debug Logs under Monitoring and set my name as Monitored User. But I cant see the desired output..
All I see under Operation is "/setup/build/editApexClass.apexp".
Am I doing something wrong here..please correct & guide me if so....
Thanks a lot
JL
Couple of questions:
1. How are you using the code? Did you do whatever it was that actually is supposed to run the code?
2. Make sure your log filters are not turned off for the classes
3. Did you refresh your page?
5. What does it say under monitored users?
Hi Starz26,
1. The code is being used part of another code but have to make sure it has the right mapping before using it anywhere else.
2.My log filters are good.I rechecked them.
3.Tried refreshing my page but no gain.
4.It has my name under the monitored users.
Hey, is writing a test case required to check the code in the debug log...if so please guide me as I don't even have the slightest idea of it...
Thanks a lot
JL
Test case is not needed, but if you have not implemented the code the only way to run it will be a test case...
To see in the debug log you WILL need to actually call the code.
Hi Starz26,
Thanks for the clarification....
can you please guide me on how to call this code without the test case in debug Log....
Thanks a lot
JL
Are you working in prod or sandbox?
You have to call the mothod that contains the code just like anything else......
Hi Starz26,
I am working in a sandbox.
Hey, Thanks again for the support.
Thanks a lot
JL