+ Start a Discussion

Add multiple child records using wrapper class

Hello, my overall goal here is to copy multiple users on any case record. 


I've created a custom object called Case Associate, which is a master/detail to Case.  The records in this object will include a lookup field to the selected user. Once the record is created on this object, an workflow will send an email to the user specified in the record.


To automate this task, I've created a custom button on the Case object called "Select Users", which brings the user to a visualforce page, which has an extension to list and select multiple users.


Once the users selects multiple users and clicks "Processed Selected", I want the Case Associate records to automatically populate with the amount of Users the user selects.  So to be more clear, the user is on a Case, clicks a button and selects 5 different users, then clicks "process selected"  They will then be redirected to the Case record and will see 5 records appear in the related list of Case Associate object.  Each of these records will contain a lookup to the user that was selected.


I altered the following code to help create this page and class, but I'm struggling on the process request method:



Any ideas on the code needed to pass the ID of the selectedUser into each record of the Case_Associate?


Below is what I've tried so far, but I get an error message:


public PageReference processSelected() {

//We create a new list of Users that we be populated only with Users if they are selected
List<User> selectedUsers = new List<User>();

//We will cycle through our list of cUsers and will check to see if the selected property is set to true, if it is we add the User to the selectedUsers list
for(cUser cUse: getUsers()) {
if(cUse.selected == true) {

// Now we have our list of selected users and can add them to the Case Associate object on Cases
 for(User use: selectedUsers) {
Case_Associate__c caseA = new Case_Associate__c (User__c=selectedUsers.Id);

Compile Error: Initial term of field expression must be a concrete SObject: LIST<User> 


insert selectedUsers;

Best Answer chosen by Admin (Salesforce Developers) 

You are assigning a list to User__c field...you should iterate through the list and concatenate the values in the User__c.

All Answers


You are assigning a list to User__c field...you should iterate through the list and concatenate the values in the User__c.

This was selected as the best answer

Thanks UV, I was able to use another post to help me out:  This wrapper class in this post is similar to what I am trying to accomplish.  It also includes a test class to get me started.