0

The code I wrote is not setting updating the field on the same window, and my other method would refresh the page, and field is not updated until I refresh the page manually.

My goal is to provide a list of options a (read-only) field, which are dynamically fetched from an api service, and when user selects one, it updates a field for the user.

the user should not be able to set the field directly, and I have to provide them with a list of options depending on what the order is. (these are on my sandbox)

my apex class/controller:

public with sharing class OrderEditController {
    public ApexPages.StandardController mController;
    private Order mOrder;
    public String method;

    public OrderEditController(ApexPages.StandardController controller) {
        mController = controller;
        mOrder = loadOrder((Order)controller.getRecord());
        setMethod(mOrder.Shipping_Method__c);
    }

    public String getSelected() {
        return mOrder.Shipping_Method__c;
    }

    public String getMethod() {
        return method;
    }
    public void setMethod(String newMethod) {
        if (newMethod.length() > 0) {
            method = newMethod;
        } else {
            method = 'NONE';
        }
        method = newMethod;
    }
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>(); // this will be dynamically created
        options.add(new SelectOption('GND', 'UPS - Ground Standard'));
        options.add(new SelectOption('3DS', 'UPS - 3 Day Select'));
        options.add(new SelectOption('2DA', 'UPS - 2nd Day Air'));
        options.add(new SelectOption('1DA', 'UPS - Next Day Air'));
        return options;
    }

    public PageReference apply() {
        mOrder.Shipping_Method__c = method;
        Database.update(mOrder);

        return mController.view();
        //return null;

        //return null; //mController.view();
        //PageReference ref = new PageReference(ApexPages.currentPage().getUrl());
        //ref.setRedirect(true);
        //return ref;
    }

    public PageReference fnUpdate() {
        updateMethod();
        return null;
    }

    // set method on order
    private void updateMethod() {
        mOrder.Shipping_Method__c = method;
        Database.update(mOrder);
    }

    // by default our fields are not loaded
    private Order loadOrder(Order ord) {
        String id = ord.Id;
        return [SELECT o.Id,o.OrderNumber,o.Shipping_Method__c FROM Order o WHERE o.Id=:id];
    }

}

my visual force page which is inside Order Layout:

<apex:page id="OrderEditOverride" standardController="Order" extensions="OrderEditController">


    <apex:form>
        <apex:selectRadio value="{!method}">
            <apex:selectOptions value="{!items}"/>
        </apex:selectRadio>
        <apex:commandButton value="Apply" action="{!apply}" rerender="out" status="status" />
    </apex:form>

    <!-- selected preview section -->
    <apex:outputPanel id="out">
        <apex:actionstatus id="status" startText="testing...">
            <apex:facet name="stop">
                <apex:outputPanel >
                    <p>Currently selected:</p>
                    <apex:outputText value="{!selected}"/>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>

</apex:page>

Edit: To clearify, how to show some radio buttons that the options are provided from controller.

  • 1
    The answer by @Ronnie was what I needed. my code was about 98% close to what I was trying to achieve, which was to update a read only/invisible field by letting user select from a dynamically generated picklist/radio-set. – Mac A. Jul 31 '18 at 18:27
1

Try moving your apply call to an action support on change - update once they submit their selection like below:

Page:

    <apex:page id="OrderEditOverride" standardController="Order" extensions="OrderEditController">
        <apex:pageMessages />

        <apex:form>
            <apex:selectRadio value="{!method}">
                <apex:selectOptions value="{!items}"/>
                <apex:actionSupport event="onchange" action="{!setSelectedMethod}" reRender="out" status="status" />
            </apex:selectRadio>
            <apex:commandButton value="Apply" action="{!apply}" id="save_btn" />
        </apex:form>

        <!-- selected preview section -->
        <apex:outputPanel id="out">
            <apex:actionstatus id="status" startText="testing...">
                <apex:facet name="stop">
                    <apex:outputPanel >
                        <p>Currently selected:</p>
                        <apex:outputText value="{!selected}"/>
                    </apex:outputPanel>
                </apex:facet>
            </apex:actionstatus>
        </apex:outputPanel>

    </apex:page>

Controller:

    public with sharing class OrderEditController {
        public ApexPages.StandardController mController { get; set; }
        private Order mOrder        { get; set; }
        public String method        { get; set; }

        public OrderEditController(ApexPages.StandardController controller) {
            mController = controller;
            mOrder = loadOrder( ( Order )controller.getRecord() );


            method = ( String.isBlank( mOrder.Shipping_Method__c ) ? 'NONE' : mOrder.Shipping_Method__c );
        }

        public String getSelected() {
            return mOrder.Shipping_Method__c;
        }

        public void setSelectedMethod(){
            mOrder.Shipping_Method__c  = method;
        }

        public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>(); // this will be dynamically created


            options.add(new SelectOption('GND', 'UPS - Ground Standard'));
            options.add(new SelectOption('3DS', 'UPS - 3 Day Select'));
            options.add(new SelectOption('2DA', 'UPS - 2nd Day Air'));
            options.add(new SelectOption('1DA', 'UPS - Next Day Air'));

            return options;
        }

        public PageReference apply() {

            mOrder.Shipping_Method__c = method;

            try{
                Database.update( mOrder );
            }
            catch( Exception ex ){
                ApexPages.addMessage( new ApexPages.Message( ApexPages.Severity.ERROR, 'Error occurred applying shipping Method: ' + ex.getMessage ));
                return null;
            }

            PageReference pg = new PageReference( '/' + mOrder.Id );
            pg.setRedirect( true );

            return pg;
        }


        // by default our fields are not loaded
        private Order loadOrder( Order ord ) {

            return [ SELECT o.Id, o.OrderNumber, o.Shipping_Method__c FROM Order o WHERE o.Id =: ord.Id ];
        }

    }

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