1

I have to be able to create a child record called "seats" when the parent of the parent object of "Seats" changes.

Teams -->Tables---> Seats

So when Team changes, I need to be able to create Seats for all the tables that are related to Teams.

I figured I should write a method that when Team changes, but how would I bring in the relationship of Tables to create the seats? I know I should query for it but I'm not sure how to get from setting my if statements to bringing in the query and then insert of seats.

   public static void makeNewSeats(Map<Id, Team> oldTeams, 
   list<Team__c> Teams){
   List<Seat__c> seatRecords = new list<Seat__c>();
   List<Table__c> tableRecords = "Query here???"();
    for(Team__c team : Teams){
        if(oldTeams.get(team.id).filled__c ==false && Team.filled__c == true){
3

Here is an approach, though I am guessing the relationship field API names.

public static void makeNewSeats(Map<Id, Team> oldTeams, 
   list<Team__c> Teams)
{
   List<Seat__c> seatRecords = new list<Seat__c>();
   Map<Id, Team__c> newMap = new Map<Id,Team__c>(Teams);

   //retrive table records based on team Ids
   Map<Id,Table__c> maptableRecords = new Map<Id,Table__c>([SELECT Id, Team__c FROM Table__c WHERE Team__c IN:newMap.keySet()]);

    for(Team__c team : Teams)
    {
        //check for which seat record to be created
        if(oldTeams.get(team.id).filled__c ==false && Team.filled__c == true)
        {
            //loop through table records
            for(Table__c tblObj:maptableRecords.values())
            {
                //compare the table record
                if(team.id == tblObj.Team__c)
                {
                    //here create Seat record.

                    Seat__c seatObj = new Seat__c();
                    seatObj.Table__c = tblObj.Id;

                    //assign relevant attributes of seat object.
                    seatRecords.add(seatObj);                   
                }               
            }//end of table for loop
        }
    }//end of for loop

    insert seatRecords;
}
  • Awesome, thanks again! – J.koh May 14 '18 at 19:05

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.