1

The command button on my Visualforce page is not saving edits to the record. Any help is appreciated!

Visualforce Pg:

<apex:page action="{!redirectToPartnerships}" extensions="EditExtensions"
           standardController="PartnershipRequest__c">
    <apex:form >

        <apex:pageBlock title="Research Request Edit">
            <!--<apex:commandButton value="Save" action="{!save}"/> 
            <apex:commandButton value="Cancel" action="{!cancel}"/>  -->
            <apex:pageBlockSection title="Request Overview (Completed by 
                      Requestor)">
                <apex:inputField value="{!requestRecord.Name}"/>
                <apex:repeat value="{!fields}" var="f">
                    <apex:inputField value="{!requestRecord[f.fieldPath]}"
                                     required="{!OR(f.required, f.dbrequired)}"/>
                </apex:repeat>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!Save}" value="Save" />
                <apex:commandButton action="{!Cancel}" value="Cancel" />
            </apex:pageBlockButtons>

        </apex:pageBlock>

    </apex:form>
</apex:page>

Apex Class:

public class EditExtensions {

    public string getRequestId { get; set; }
    public PartnershipRequest__c requestRecord { get; set; }
    public pageReference redirectReference;

    public yc_PartnershipEditExtensions(ApexPages.StandardController controller) {
        requestRecord = (PartnershipRequest__c) controller.getRecord();
        this.getRequestId = ApexPages.CurrentPage().getparameters().get('Id');
        this.requestRecord = getRequest();
        this.redirectReference = null;
        //  requestRecord = [select DataTool__c, Agency__c, Client__c,
        Description__c, DesiredDueDate__c, PriorityRanking__c, ImpactCategory__c
        from PartnershipRequest__c where id=:getRequestId];
    }

    private PartnershipRequest__c getRequest() {
        String query = 'SELECT ';
        for (Schema.FieldSetMember f : this.getFields()) {
            query += f.getFieldPath() + ', ';
        }
        query += 'Id, Name FROM PartnershipRequest__c WHERE id=:getRequestId';
        //query += this.getRequestId;
        System.debug(Database.query(query));
        return Database.query(query);
    }

    public List<Schema.FieldSetMember> getFields() {
        Map<String, Schema.RecordTypeInfo> recordTypeMap =
                Schema.SObjectType.PartnershipRequest__c.getRecordTypeInfosByName();
        System.debug('reqrec' + this.requestRecord);
        if (this.requestRecord.RecordTypeId == recordTypeMap.get('General Request').getRecordTypeId()) {
            //general request
            return SObjectType.PartnershipRequest__c.FieldSets.General_Request.getFields();
        } else if (this.requestRecord.RecordTypeId ==
                recordTypeMap.get('Troubleshooting Data Vendors').getRecordTypeId()) {
            //troubleshooting data vendors
            return
                    SObjectType.PartnershipRequest__c.FieldSets.Troubleshooting_Data_Vendors.getFields();
        } else if (this.requestRecord.RecordTypeId ==
                recordTypeMap.get('Sandbox').getRecordTypeId()) {
            //sandbox
            return SObjectType.PartnershipRequest__c.FieldSets.Sandbox.getFields();
        } else if (this.requestRecord.RecordTypeId == recordTypeMap.get('Partnerships').getRecordTypeId()) {
            //if field type is partnerships, use existing VF pages for Create / Edit / View (making the return below invalid. There are no field sets being used here.)
            //return
            SObjectType.PartnershipRequest__c.FieldSets.Partnerships.getFields();
            System.Debug('is this working?');
            //redirectReference = redirectToPartnerships();
            return SObjectType.PartnershipRequest__c.FieldSets.Troubleshooting_Data_Vendors.getFields();
        }

        //System.debug(this.requestRecord.RecordTypeId);
        return SObjectType.PartnershipRequest__c.FieldSets.Troubleshooting_Data_Vendors.getFields();
    }

    public PageReference redirectToPartnerships() {
        Map<String, Schema.RecordTypeInfo> recordTypeMap =
                Schema.SObjectType.PartnershipRequest__c.getRecordTypeInfosByName();
        if (this.requestRecord.RecordTypeId ==
                recordTypeMap.get('Partnerships').getRecordTypeId()) {
            system.debug('redirect to partnerships');
            PageReference redirectRef = new


                    PageReference('https://c.cs21.visual.force.com/apex/PartnershipRequest_EditVF?
                    id=' + this.getRequestId);
                    redirectRef.setRedirect(true);
            return redirectRef;
        }

        return null;
    }
}
  • Wnich command? What do you mean by "no saving"? Are there errors? We need details... – Sebastian Kessel Apr 17 at 21:37
  • can you add an <apex:pageMessages/> tag to your page? It should surface any errors that might be thrown by the standard controller – Sebastian Kessel Apr 17 at 21:41
  • no errors, when I update a field and click the "Save" button, the changes are not committed. – FWSFDC Apr 17 at 21:41
  • See my 2nd comment above.... – Sebastian Kessel Apr 17 at 21:42
  • 1
    @SebastianKessel Don't worry, I got this one. Give me a min. – sfdcfox Apr 17 at 21:43
2

The "save" function you're using is attached to the StandardController. However, the record you're attaching everything to is not the record in the standard controller. Your problems start here:

    requestRecord = (PartnershipRequest__c) controller.getRecord();
    this.getRequestId = ApexPages.CurrentPage().getparameters().get('Id');
    // This is **not** from the standard controller
    this.requestRecord = getRequest();

Instead, you'll want to build your query and copy the values from the query to the standard controller record, or use the addFields method to load the necessary data.


public yc_PartnershipEditExtensions(ApexPages.StandardController controller) {
    String[] fields = new String[0];
    for (Schema.FieldSetMember f : this.getFields()) {
        fields.add(f.getFieldPath());
    }
    // This is not legal in unit tests, so skip...
    if(!Test.isRunningTest()) {
        controller.addFields(fields);
    }
    // now load record...
    requestRecord = (PartnershipRequest__c) controller.getRecord();
    getRequestId = requestRecord.Id;
}

Everything else should be okay from here.

  • I've tried updating the class with you code snippet and receive the following error: SObject row was retrieved via SOQL without querying the requested field: PartnershipRequest__c.Name – FWSFDC Apr 18 at 15:31
  • @FWSFDC Because of how your VF is written, you'll need to also add the Name field. – sfdcfox Apr 18 at 15:36
  • Not sure I follow? Where would the Name field have to be specified in the Visualforce page@ @sfdcfox – FWSFDC Apr 19 at 13:19
  • @FWSFDC You can just add the Name field to the fields list. – sfdcfox Apr 19 at 15:26
  • do you mean add the Name field to the field set? there is no fields list in the VF page or Apex Class @sfdcfox – FWSFDC Apr 21 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.