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
MarkHBMarkHB 

Hardcoding fields in List Actions with StandardSetController

Hi,
I'm trying to create a List Action Visualforce page (Lightning environment) which will not only include two user-set fields, but also push in the user's ID and current timestamp into two additional fields which the user has no control over.  Updating the selected records with the exposed fields (a dropdown and a textbox) works fine, but I have been unable to set the other two fields.  I have tried two different tactics:
  1. Using inputHidden for the two fields (see code below), but I haven't found the magic Javascript incantation to properly set them before the form submits.  In ordinary HTML I would just set the value attribute, but that's bound to the object field and using html-value is not allowed.
  2. Using controller.getRecord() in the controller extension, also as shown below, but that doesn't work, either; I can tell in the logs that updateRecords is called but the fields aren't updated.
Does anyone have a working solution for this?  (Note: I haven't tried both methods at the same time, I just wanted to show them both in the example).

Page:
<apex:page standardController="MyObject__c"
        recordSetVar="adjustments"
        extensions="ExtensionsClass"
        showHeader="false"
        id="MyObject_BulkChange">
    <apex:slds />
    <apex:form id="muform">        
        <apex:pageBlock title="Refunds / Adjustments Mass-Update" mode="edit" id="mub1">
            <apex:pageMessages />
            <apex:pageBlockSection id="mus1">
                <apex:inputField value="{!MyObject__c.Status__c}" id="status">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputField>
                <apex:inputField value="{!MyObject__c.Comment__c}" id="comment">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputField>
                <apex:inputHidden value="{!MyObject__c.User_Name__c}" rendered="true" id="userName"/>
                <apex:inputHidden value="{!MyObject__c.Action_Date__c}" rendered="false" id="actionDate"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom" id="mubut">
                <apex:commandButton value="Save" action="{!updateRecords}" id="butsav"/>
                <apex:commandButton value="Cancel" action="{!cancel}" id="butcan"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:pageBlock title="Selected Items" id="muselectedlist">
            <apex:pageBlockTable value="{!selected}" var="ref" id="mutab">
                <apex:column value="{!ref.Account_Number__c}" id="refAcctNbr"/>
                <apex:column value="{!ref.Name}" id="refName"/>
                <apex:column value="{!ref.Status__c}" id="refStatus"/>
                <apex:column value="{!ref.Comment__c}" id="refComment"/>
                <apex:column value="{!ref.User_Name__c}" id="refUserName"/>
                <apex:column value="{!ref.Action_Date__c}" id="refActionDate"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public with sharing class ExtensionsClass {
    ApexPages.StandardSetController setCon;

    public selectedSizeWorkaround(ApexPages.StandardSetController controller) {
        setCon = controller;
    }

    public integer getMySelectedSize() {
        return setCon.getSelected().size();
    }
    public integer getMyRecordsSize() {
        return setCon.getRecords().size();
    }

    public PageReference updateRecords() {
        ((MyObject__c)(setCon.getRecord())).User_Name__c = UserInfo.getUserId();
        ((MyObject__c)(setCon.getRecord())).Action_Date__c = System.now();
        return setCon.save();
    }
}

Thanks in advance for any help.