[SalesForce] How to save updates to a related record on a visualforce page

I have a visual force page that dynamically displays records attached to a case. When edited I need these to be updated when a user presses save. Right now it is not saving.

Visualforce page:

<apex:repeat value="{!Case.Vitality_Check_Attributes__r}" var="att">
            <td class="text-right">
                <apex:outputLabel value="Name" for="name"/>
                <apex:inputField value="{!att.name}" id="name"/>
                <br></br>
                <apex:outputLabel value="Assesment Group ID" for="agi"/>
                <apex:inputField value="{!att.Assessment_Group_ID__c}" id="agi"/>
                <br></br>
                <apex:outputLabel value="Assesment Identifier" for="ai"/>
                <apex:inputField value="{!att.Assessment_Identifier__c}" id="ai"/>
                <br></br>
                <apex:outputLabel value="Assesment ID" for="aid"/>
                <apex:inputField value="{!att.Attribute_ID__c}" id="aid"/>
                <br></br>
                <apex:outputLabel value="Attribute Identifier" for="ati"/>
                <apex:inputField value="{!att.Attribute_Identifier__c}" id="ati"/>
                <br></br>
                <apex:outputLabel value="Attribute" for="atr"/>
                <apex:inputField value="{!att.Attribute_Value__c}" id="atr"/>
                <br></br>
                <apex:outputLabel value="Source Type" for="src"/>
                <apex:inputField value="{!att.Source_Type__c}" id="src"/>
                <br></br>
                <apex:commandButton action="{!AttSave}" value="Save"/>
            </td>
</apex:repeat>

Controller Extension:

public void AttSave(){
    List<Vitality_Check_Attribute__c> attr = [SELECT Name, Id, Assessment_Group_ID__c, Assessment_Identifier__c, Attribute_ID__c, Attribute_Identifier__c, Attribute_Value__c, Case__c, Source_Type__c FROM Vitality_Check_Attribute__c WHERE Case__c = :CaseID];
    System.debug('Size: ' + attr.size());
    for(Vitality_Check_Attribute__c vca : attr){
        System.debug('This is a debug log: ' + attr.size());
        update vca;
    }
}

Best Answer

When you save, the updated values will be automatically set in the collection that you presented in the page. You can get a reference to that through the standard controller (or if you are not using a standard controller through however you queried the Case in the first place) and then just update the list:

private ApexPages.StandardController sc;

public MyClass(ApexPages.StandardController sc) {
    this.sc = sc;
}

public void AttSave() {
    Case c = (Case) sc.getRecord();
    update c.Vitality_Check_Attributes__r;
}
Related Topic