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
chubsubchubsub 

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:

http://wiki.developerforce.com/page/Wrapper_Class

 

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) {
selectedUsers.add(cUse.use);
}
}

// 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) 
UVUV

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

UVUV

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
chubsubchubsub

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. 

 

http://boards.developerforce.com/t5/Apex-Code-Development/wrapper-class-test-woes/m-p/330707/highlight/false#M58567