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
Dave BerenatoDave Berenato 

Save Related Records for Standard Set Controller

I have a Standard Set Controller for a Visualforce Page. The code for the "SaveandReturn" works for saving the main record (the FAIR_Listing__c) but it does not work for saving any of the Lookup Records' fields (Charlie_5_Lookup__r.Agent_Photos_Mandatory_Day_4__c). What is the code to save a Lookup Record in a Standard Set Controller.
public class Pipeline {

    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(                  
                    [Select Name, 
                     Id, OwnerId, 
                     Status__c,
                     Bankruptcy_Lookup__r.Id,  
                     Charlie_5_Lookup__r.Agent_Photos_Mandatory_Day_4__c, 
                     Charlie_5_Lookup__r.Id,
                     From FAIR_Listing__c WHERE Status__c = 'Active' or Status__c = 'Hold Short Sale' or Status__c = 'Hold Standard Sale']));
            }
            return setCon;
        }
        set;
    }
    
    public List<FAIR_Listing__c> getRecords() {
        return (List<FAIR_Listing__c>) setCon.getRecords();
    }
    
	public void saveandReturn() {
	setcon.save();
	}
}
For a repeating table on a Visualforce page:
 
<apex:page Controller="Pipeline">
    <apex:form >
        <apex:pageBlock >
    <html>
        <head>
            <style>
                
            </style>
        </head>
        <body>                       
            <apex:repeat value="{!records}" var="record">
                <table border = "1" cellpadding = "0" cellspacing = "0">
                    <tr>
                        <th width="100px">Name</th>
                        <th width="200px">Status</th>
                        <th width="100px">Agent Photos</th>
                    </tr>
                    <tr>
                        <td><apex:outputField value="{! record.Name }"/></td>
                        <td><apex:inputField value="{! record.Status__c }"/></td>
                        <td><apex:inputField value="{! record.Charlie_5_Lookup__r.Agent_Photos_Mandatory_Day_4__c }"/></td>
                    </tr>
                </table>
                <br/>
            </apex:repeat>
        </body>
    </html>
      <apex:pageBlockButtons >
            <apex:commandButton action="{!saveAndReturn}" value="Save"/>
      </apex:pageBlockButtons>
     </apex:pageBlock>
    </apex:form>
    </apex:page>

 
Best Answer chosen by Dave Berenato
Alain CabonAlain Cabon
Hi,

Dave is right and the solution is like that:
public void saveAndReturn() {
     //   system.debug('id: ' + ((List<FAIR_Listing__c>)setcon.getRecords())[0].Bankruptcy_Lookup__r.Id);
        for (FAIR_Listing__c mylist:(List<FAIR_Listing__c>)setcon.getRecords()) {
            update mylist.Bankruptcy_Lookup__r;
            update mylist.Charlie_5_Lookup__r;
        }
        setCon.save();      
}
Updating Related Records​
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_insert_update.htm

 

All Answers

Damon ShawDamon Shaw
Hi Dave,

I can't say I've use Standard Set Controllers that often but I would only expect it to save the records you are dealing with (FAIR_Listing__c) not any related records like the record linked to via Charlie_5_Lookup__r

a quick google pretty much says the same thing, the save method "Inserts new records or updates existing records that have been changed", it doesn't mention related records too

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_ApexPages_StandardSetController_save.htm

I think if you want to update the Charlie_5_Lookup__r record you will need to look that record up and update it directly
Alain CabonAlain Cabon
Hi,

Dave is right and the solution is like that:
public void saveAndReturn() {
     //   system.debug('id: ' + ((List<FAIR_Listing__c>)setcon.getRecords())[0].Bankruptcy_Lookup__r.Id);
        for (FAIR_Listing__c mylist:(List<FAIR_Listing__c>)setcon.getRecords()) {
            update mylist.Bankruptcy_Lookup__r;
            update mylist.Charlie_5_Lookup__r;
        }
        setCon.save();      
}
Updating Related Records​
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_insert_update.htm

 
This was selected as the best answer
Alain CabonAlain Cabon
By the way, I have posted another solution here: https://developer.salesforce.com/forums/ForumsMain?id=9060G000000MQk4QAG
Damon ShawDamon Shaw

Thanks for the link Alain,

​it does say however that updating the related records requires a separate DML call, doing that in a loop for each record in the set is not a good idea, once your set gets too big you will hit the DML limit.

I think putting the related records into their own maps, looping over FAIR_Listing__c records and using those to update the related records, then update the set and each of the Map.values() lists will result in just 3 DML calls rather than potentially hitting the limit