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
bakumbakum 

help a PHP guy with using maps!

Hi,

I've got two objects...Logistics_Event__c (LE__c) and Logistics_Event_Instructor__c(LEI__c).

LEI__c looks up LE__c, that is to say LEI__c is n:1 with LE__c.

I am writing a trigger on LE and testing for certain conditions.  When those conditions are met, I need to find all the associated LEI records and update a field (Update_Flags__c) on them.  This is my plan:

1)Create a set of all the LE__c's associated with the trigger.
    Set<Id> allLE = new Set<Id>();

    //create a set of all the Logistics_Events in the trigger
    for(Logistics_Event__c leTrigger:Trigger.new)
    {
      allLE.add(leTrigger.Id);
    }


2)Create a map of all the LEI__c's associated with those LE__c's.
    //use one query to find all the LEI's that are associated with the LE's in the trigger
    Map<Id, Logistics_Event_Instructor__c> leimap = new Map<Id,  Logistics_Event_Instructor__c>([SELECT lei.Id,lei.Event__c,lei.Update_Flags__c
                                FROM Logistics_Event_Instructor__c lei
                                WHERE Event__c in :allLe]);
(lei.Event__c is lookup to LE__c)

3)Using a for loop to iterate through the triggers do the testing

4)When testing finds a true value, search the map for all LEI's where Event__c = trigger.new[i].Id and add those LEI's to a list which will be updated in one DML operation.  This is where I start to lose the plot...

As I understand it my Map is an array where the key is the ID of LEI and the value is the actually LEI object itself.  If this were PHP I would simply loop through the array with foreach() and check the values in the object using object notation (i.e. objectname-->fieldname), but I don't understand how to do this in Apex.

QUESTIONS:
Is there a method that can search in the Value portion of the map for a certain field, if the value is an sObject? If there is, and if the return would be multiple rows in the map, how do I deal with them?

Or, is there a way to iterate through the map itself (cumbersome, I know) and then examine a field on the object?   It's probably some For loop syntax that is like Java 101 but I don't get it, and the Apex docs are certainly no help.

What about nested maps?  In PHP I can easily use multivariate arrays, and it seems nested maps are analagous, but the Apex docs don't include a shred of help in the syntax of using map methods with nested maps and it's not intuitive at all.  Maybe it is to someone else, I dunno...

Any other way anyone would suggest?

Thanks!
-mb