Introduction

There are plenty of times where a user might want to generate documents for each item in one of the related lists on the current record they're viewing. For example, the user may want to click a button on an Opportunity Record that sends an email to each Contact Role related to that Opportunity, or the user might want to click a button on an Account record that generates an Opportunity Summary for each Opportunity record related to that Account. Setting up this feature is as simple as copying and pasting the following code into your org.

Note: You can also generate a single document with information merged in from each record in a related list that can be formatted in any way you'd like. This feature utilizes S-Docs component templates. Click here to read more about this feature.

Step 1: Create Apex Class

Head over to Setup > Build > Develop > Apex Classes > New and copy/paste the following code into your new Apex class, then click Save.

Step 2: Create Visualforce Page

Head over to Setup > Build > Develop > Visualforce Pages > New and copy/paste the following code into your new Visualforce Page. Name the page SDRelatedListDocuments (i.e. enter this value into both the "Label" and "Name" fields), then click Save.

Step 3: Create Record Detail Button(s)

For each object that requires this functionality, you'll need to create a button similar to the following example created for emailing a document to each Contact Role related to an Opportunity.
Here's what each query parameter denotes:
parentId: The ID of the record that users will see this button on. For example, if your users will click a button on the Opportunity record detail page that creates and emails a document for each Contact Role, this value should be {!Opportunity.Id}. childObjName: The API name of the object type of the records in the related list. For example, if your users will click a button on the Opportunity record detail page that creates and emails a document for each Contact Role, this value should be OpportunityContactRole.
lookupFieldName: The API name of the field relating the related list object to the parent object that the user will see this button on. For example, if your users will click a button on the Opportunity record detail page that creates and emails a document for each Contact Role, this value should be Opportunity because a Contact Role record is related to its parent Opportunity record via a lookup field with API name Opportunity (e.g. if you wanted to query the IDs of all the Opportunities related to some Opportunity Contact Role with ID aXM1F000000N7XL, you would run the query SELECT Opportunity.Id FROM OpportunityContactRole WHERE Id='aXM1F000000N7XL' - notice how the Opportunity ID is accessed from the Opportunity Contact Role record through a lookup field with API name "Opportunity").
doclist: A comma-delimited list of S-Docs Template names or IDs that will be used to generate the related list documents. These S-Docs Template records should have the Related To Type set to the object of the related list records, rather than the object of the parent records. For example, if your users will click a button on the Account record detail page that creates a document for each Opportunity, this should be Opportunity, rather than Account. Going back to our Opportunity Contact Roles case is a bit more interesting: if your users will click a button on the Opportunity record detail page that creates and emails a document for each Contact Role, this shouldn't be Opportunity, of course, but it also shouldn't be OpportunityContactRole as you might have expected; rather, it should be Contact, since OpportunityContactRole is really just a relationship record linking a Contact (where the real meat of the Opportunity Contact Role data lies) to an Opportunity.
allowEmail (optional): Set this to 1 if you want to allow the user to email the generated documents. Set this to 0 or omit it altogether if you don't want to allow the user to email the generated documents.
aid (optional): A comma-delimited list of the Salesforce Attachments that you would like to attach to each email (note that emails can only be sent out if you set allowEmail to 1).
did (optional):
 A comma-delimited list of the Salesforce Documents that you would like to attach to each email (note that emails can only be sent out if you set allowEmail to 1).
sdocSortFields (optional): Field (or fields) on the S-Doc or S-Docs Template object to sort the documents by. In the example above, this sorts by template name.
objSortFields (optional): Field (or fields) on the base object to sort the documents by. In the example above, this sorts by Contact Last Name.
combineAll=[true|file|attachment] (optional): If this is set to true, all documents will be combined into a single PDF that can be saved as either a file or attachment.

Note: The combineall functionality will throw an error if you're not on S-Docs version 2.944 or above.

autoRedirect=[record|combineall|email|email,record] (optional): This parameter will automatically redirect you depending on which of the four values you input. Record will redirect you back to the record you started on. Combineall will take you to the combined PDF document if you've set the comebineall parameter to true. Email will take you to the email page so you can edit the email fields before sending your documents. Email,record will automatically email the documents and redirect you back to the record you started on.

Step 4: Create Test Class

Head over to Setup > Build > Develop > Apex Classes > New and copy/paste the following code into your new Apex class, then click Save.