0
<apex:page controller="test">
    <apex:includeScript value="/soap/ajax/40.0/connection.js"/>

    <script type="text/javascript">

    function CheckUI()
   {

     loadAndGo();

    } 
    </script>

    <apex:sectionHeader title="test for {!forObject} {!forName}">
        <apex:form id="TheForm">
            <apex:pageBlock title="Select BLUE Type then click Go:" id="ThePageBlock">
                <apex:pageBlockButtons location="bottom">
                    <apex:commandButton value="Cancel" immediate="true" action="{!cancel}" />
                    <apex:commandButton value="Go" onClick="CheckUI();" />
                </apex:pageBlockButtons>
                <apex:selectList value="{!selRecTypeId}" multiselect="false" size="1">
                    <apex:selectOptions value="{!RecordTypes}"/>
                </apex:selectList>
                <br/>
                <apex:messages />
            </apex:pageBlock>
            <apex:pageBlock title="Atest:" id="TheLegendBlock">
                <apex:outputPanel title="Available test Record Types">
                    <table style="border:1px solid black">
                        <apex:repeat value="{!recTypes}" var="rt">
                            <tr style="border:1px solid black">
                                <td style="border:1PX solid black">
                                    <apex:outputField value="{!rt.Name}" />
                                    &nbsp;
                                </td>
                                <td style="border:1PX solid black">
                                    &nbsp;
                                    <apex:outputField value="{!rt.Description}" /></td>
                            </tr>
                        </apex:repeat>
                    </table>
                </apex:outputPanel>
            </apex:pageBlock>
        </apex:form>
    </apex:sectionHeader>

Now the apex method itself works, i know because the redirect works when the command button is set like this

<apex:commandButton value="Go" action="{!loadAndGo}" rerender="ThePageBlock" />

But now i have a new use case, i have to use $User.UIThemeDisplayed and check if the user is in classic or lightning, if classic then it'll run loadAndGo() which is the classic apex method, if not it will run a different apex method, which will then have to return data and run that through sforce.one.createRecord...but that's beyond the scope of this question.

Right now i'm trying to get the apex method to simply work when called from JS...now the screen 'refreshes' on click but nothing happens no redirect occurs. Here's part of the method.

     public pageReference loadAndGo() {
        // Error message if no record type selected:
        pageReference goPage = null;
        if(selRecTypeID == null) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'You must select a Blue type before clicking Go.'));
        } else {  ****** compile the url here and other logic******

}
}
  pageReference newPage = new pageReference(newURL);
            goPage = newPage;
            goPage.setRedirect(true);

        }
        return goPage;
}
1

The button's default action needs to be canceled, and you need to use an apex:actionFunction to affect a redirect.

<apex:actionFunction reRender="none" action="{!loadAndGo}" name="loadAndGo" />

...

<apex:commandButton value="Go" onClick="CheckUI(); return false;" />

...

As an aside, if you're just interested in a potential redirect on page load, you could just do so on the page itself:

<apex:page action="{!loadAndGo}" ... />
  • thank you that fixed it, and no the apex:page route wouldn't work as my flow will be; onClick run js if user is in lightning UI or classic UI. If classic -> hacky method, if lightning -> public string method that will return JSON.serialized(array) then use sforce.one.createRecord...Now i'm an idiot; i realized sforce.one.createRecord works in classic. i'll probably get rid of the hack method and just use sforce.one.create, but i be able to easily use the array retrieved via the return string method (return JSON.serialized(array) ) to fill in the defaultFieldValues – ensir Jun 3 at 19:23
  • Just wondering if i'm digging myself a hole with that. – ensir Jun 3 at 19:23
  • @ensir That sounds reasonable. Just remember that you'll need a different redirect for classic and not in console. – sfdcfox Jun 3 at 19: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.