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
Mona WesoMona Weso 

How to create a list of new child records from a list of parent records in Apex

I am trying to pass an opportunity record collection from a flow into an Apex Action that will loop through the opportunity record collection (LIST), then create an opportunity team member record for each opportunity in that list, then return the new list as a new opportunity team member collection that will be an output variable accessable via the flow.  However, I keep getting the following errors that I cannot resolve.  I am new to Apex so I am guessing that my MAP setup is wrong.

 

Illegal assignment from List<Opportunity> to List<OpportunityService1.ReturnVariables>

Illegal assignment from OpportunityTeamMember to OpportunityService1.ReturnVariables

//invocable method to create OpportunityTeamMember records from a list of Opportunity Ids
    @InvocableMethod(label='Create OTM Records for OOO' description='Given a list of Opportunity IDs, create OTM records from these IDs.')
        public static List<ListReturnVariables> createOTMRecords(List<InputVariables> inputVariables) {

            //inputs
            List<Opportunity> opps = inputVariables.get(0).opportunities;
            String varUserId;
            

            //outputs       
            
            Map<Id, Opportunity> opp = new Map<Id, Opportunity>(opps);
            List<ReturnVariables> OTMOutputs =[SELECT Id FROM Opportunity WHERE Id IN: opp.keySet()];
            for(Opportunity o : opp.values()){
            ReturnVariables otm = new OpportunityTeamMember(OpportunityId = o.Id, UserId = varUserId, OpportunityAccessLevel = 'Edit',TeamMemberRole = 'Mortgage Banker');
                OTMOutputs.add(otm);
            }
            return OTMOutputs;
        }
        public class InputVariables {
            @InvocableVariable
            public List <Opportunity> opportunities;

            @InvocableVariable
            public String varUserId;
        }
        public class ReturnVariables {
            @InvocableVariable
            public List <OpportunityTeamMember> OTMOutputs;
        }
Alex Toba 19Alex Toba 19
To create a list of new child records from a list of parent records in Apex, you can use the following approach:
1. Define a list of child records (OpportunityTeamMember) to hold the new records:

List<OpportunityTeamMember> newTeamMembers = new List<OpportunityTeamMember>();

2.  Loop through the list of parent records (Opportunity) that you received from the Flow:

for (Opportunity opp : opportunities) {
    // Create an opportunity team member record for each opportunity
    OpportunityTeamMember teamMember = new OpportunityTeamMember();
    teamMember.OpportunityId = opp.Id;
    teamMember.UserId = '005xxxxxxxxxxxx'; // Replace with the User Id of the team member
    teamMember.TeamMemberRole = 'Sales Rep'; // Replace with the role of the team member
    newTeamMembers.add(teamMember); // Add the new record to the list
}

3. Return the list of new child records:

return newTeamMembers;

Make sure that you have defined the input and output variables correctly in your Apex Action, and that you are passing the list of parent records correctly from the Flow to the Apex Action.
Regarding the errors that you are receiving, without seeing the specific error messages and the code that you have written, it's difficult to provide a specific solution. However, as you mentioned, it's possible that the issue is related to the way you are setting up the map. Make sure that you are using the correct data types for the keys and values, and that you are accessing the values correctly in your code. If you provide more specific information about the errors that you are seeing and the code that you have written, I may be able to provide more specific guidance.
CC: https://trendynewsmagazine.com/best-chinese-buffet-near-me/