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
Eric Blaxton 11Eric Blaxton 11 

Lightning Visual Force Command button behavior

Hi and thanks in advance.  

Converted to Lightning and having issues with VF command button.  

I've done research and cannot figure this one out.  I could create a lightning component, but don't want to change things too much.

Classic Behavior: the VFP resides in a page.  When the command button "add" is clicked, the page is does its thing and is refreshed within the parent page

Lightning Behavior:  the VFP looks great, but when the command button is clicked, a new window is opened up and I cannot get back to the parent Case easily.  I would like the same behavior as classic, if possible.

VF page
User-added image

VF code 
<apex:page tabStyle="Case" standardController="Case" extensions="AddFuelTypes"   lightningstylesheets="true">
    <!-- Case -->
    <style>
        .apexp .bPageBlock.apexDefaultPageBlock .pbBody .pbSubheader { 
            background-color: transparent;
            color: Black
        }
        body .bPageBlock, body #bodyCell .bResource .secondaryPalette, body .secondaryPalette.bPageBlock, body .individualPalette .secondaryPalette.bPageBlock, body .bodyDiv .genericTable, body .genericPageBlockTable, body .bodyDiv .bSubBlock, body .bComponentBlock .bPageBlock, body .bMyDashboard .bPageBlock, body.rlHoverFrame .bPageBlock, body.subjectSelectionPopup div.choicesBox, body.lookupTab .secondaryPalette.bPageBlock, body.popupTab .secondaryPalette.bPageBlock, body.UserTagStatsPage .secondaryPalette.bPageBlock {
            background-color: transparent;
            color: Black
        }
    </style>
    
    <apex:pageBlock >
        <apex:pageBlockSection title="Fuel Delivery Summary" columns="2" >
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Total Gallons Rq" style="font-weight:bold"/>
                <apex:outputField value="{!caseObj.Total_Gallons_Rq__c}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Carrier Confirm Date/Time" style="font-weight:bold"/>
                <apex:outputField value="{!caseObj.Carrier_Confirm_Date_Time__c}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Total Gallons Delivered" style="font-weight:bold"/>
                <apex:outputField value="{!caseObj.Total_Gallons_Delivered__c}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Delivered Date/Time" style="font-weight:bold"/>
                <apex:outputField value="{!caseObj.Delivered_Time__c}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Fuel Order Variance" style="font-weight:bold"/>
                <apex:outputField value="{!caseObj.Fuel_Order_Variance__c}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Fuel Delivery Time" style="font-weight:bold"/>
                <apex:outputField value="{!caseObj.Fuel_Delivery_Time__c}"/>
            </apex:pageBlockSectionItem>
            
        </apex:pageBlockSection>
    </apex:pageBlock>
    
    <!-- Add FuelType form -->
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockSection title="Add Fuel Types" columns="3">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Fuel Type" style="font-weight:bold"/>
                    <apex:outputPanel styleClass="requiredInput" layout="block" id="pwPanel" >
                        <apex:outputPanel styleClass="requiredBlock"/>
                        <apex:selectList value="{!fuelObj.Fuel_Type__c}" size="1" required="true">
                            <apex:selectOptions value="{!FuelTypeOptions}"/>
                        </apex:selectList>
                    </apex:outputPanel>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Requested Gallons" style="font-weight:bold"/>
                    <apex:inputField value="{!fuelObj.Requested_Gallons__c}" required="true"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <div align="Center" draggable="false" >
                <apex:commandButton action="{!saveFuelType}" value="Add" reRender="" onclick="redirectTo('/apex/Add_Fuel_Types','');return true;" />
            </div>            
        </apex:pageBlock>
    </apex:form>
    <apex:form >
        <div name='rerender' style="overflow-x:auto; height: 200px;">
            <apex:pageBlock >
                <apex:pageBlockTable value="{!fuelTypes}" var="ft">
                    <apex:column value="{!ft.Fuel_Type__c}"/>
                    <apex:column value="{!ft.Requested_Gallons__c}"/>
                    <apex:column value="{!ft.Delivered_Gallons__c}"/>
                    <apex:column value="{!ft.Gallon_Variance__c}"/>
                    <apex:column value="{!ft.CreatedDate}"/>
                    <apex:column >
                        <apex:commandLink value="Remove"  action="{!removeFuelType}" >
                            <apex:param name="selectedFuelTypeId" value="{!ft.Id}"/>
                        </apex:commandLink>
                    </apex:column>
                </apex:pageBlockTable>
                  <apex:outputText rendered="{!fuelTypes.size==0}">
                      <table width="100%">
                          <tr ><th>Fuel Type</th>
                          <th>Requested Gallons</th>
                          <th>Delivered Gallons</th>
                          <th>Gallon Variance</th>
                          <th>Created Date</th>
                          </tr> 
                          <tr></tr>
                          <tr><td colspan="5" style="text-align:center;">No Records</td></tr>
                      </table>
                  </apex:outputText> 
            </apex:pageBlock>
        </div>
    </apex:form>    
</apex:page>

APEX Class
 
public class AddFuelTypes {
    public string result {get; set;}
    public Case caseObj {get; set;}
    public Fuel_Type__c fuelObj {get; set;}
    public List<Fuel_Type__c> fuelTypes {get; set;}
    public string selectedFuelTypeId {get; set;}      // Selected fuel Type
    public boolean haveFuelTypes {get; set;}    // Flag to display no schedule error message

    
    public AddFuelTypes(ApexPages.StandardController stdController){   
        this.caseObj = (Case)stdController.getRecord();  // Gets the current case working on.  Case has to be created first then add fuel types
        caseObj = [SELECT Old_Total_Gallons_Rq__c,SendtoTMW__c, Carrier_Confirm_Date_Time__c, Fuel_Order_Status__c, Fuel_Order_Variance__c, Delivered_Time__c,
                   Total_Gallons_Delivered__c, Total_Gallons_Rq__c,Fuel_Delivery_Time__c
                   FROM Case 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('Id')];
        
        // Get fuel order information for current case
        fuelTypes = [SELECT Id, Fuel_Type__c, Requested_Gallons__c, Delivered_Gallons__c, 
                     Gallon_Variance__c, CreatedDate
                     FROM Fuel_Type__c 
                     WHERE Case__c = :caseObj.Id];
        
        fuelObj = new Fuel_Type__c();
        
        haveFuelTypes = false;
        if(fuelTypes.size() > 0) // Variable havefuelTypes starts as false.  Once fuel type added set to true
        {
            haveFuelTypes = true;
        }
        
        // Error messages
        selectedFuelTypeId = '';
        String temp=ApexPages.currentPage().getParameters().get('Status');
        if(temp!=NULL){
            if(temp.equals('false'))
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Duplicate fuel type cannot be entered. Please remove an existing type to add again.'));
            else if(temp.equals('CannotDelete'))
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Unable to delete, order is already Sent to TMW!'));
            else if(temp.equals('CannotAdd'))
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Unable to add, order is already Sent to TMW!'));
            else if(temp.equals('CannotBelower'))
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Please enter a valid quantity larger than 0'));
            else if(temp.equals('CannotBeHigher'))
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Please enter a valid smaller quantity'));
            else if(temp.equals('NotAllowed'))
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Please select a valid Fuel Type'));
        }
    }
    
    public List<SelectOption> getFuelTypeOptions(){
       
       List<SelectOption> options = new List<SelectOption>();
       Schema.DescribeFieldResult fieldResult = Fuel_Type__c.Fuel_Type__c.getDescribe();
       List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
       for( Schema.PicklistEntry f : ple)
       {
               options.add(new SelectOption(f.getLabel(), f.getValue()));
       }     
       return options;
    }
    
    public PageReference saveFuelType(){
        
        result='true';
        fuelObj.Case__c = caseObj.id;
        if(fuelObj.Fuel_Type__c!=NULL){
            if(fuelObj.Fuel_Type__c.equals('None'))  // Error checking, Fuel Type cannot be None
                result='NotAllowed';}
        fuelTypes = [SELECT Id, Fuel_Type__c, Requested_Gallons__c, Delivered_Gallons__c, 
                     Gallon_Variance__c, CreatedDate
                     FROM Fuel_Type__c 
                     WHERE Case__c = :caseObj.Id];
        for(Fuel_Type__c ft : fueltypes) 
        {
          if(ft.Fuel_Type__c==fuelObj.Fuel_Type__c)
              result='false';
        }
        
            if(caseObj.SendtoTMW__c!=true)
            {   
                if(!result.equals('NotAllowed')){
                    if(result.equals('true')){  // Requested Gallons must be greater than 0 and less than 100,000 gallons.  Else throw error
                        if(fuelObj.Requested_Gallons__c>=1)
                        {   
                            if(fuelObj.Requested_Gallons__c<100000)
                                insert fuelObj;
                            else
                                result='CannotBeHigher';
                        }else
                            result='CannotBelower';
                    }else
                        result='false';
                }
            }
            else
                result='CannotAdd';
        // Redirect back to same page
        PageReference reRend = new PageReference('/apex/Add_Fuel_Types?id='+ caseObj.Id+'&Status='+result);
        reRend.setRedirect(true);
        return reRend;
    }
    
    public PageReference removeFuelType(){   //Error checking when Removing fuel type.  Fuel Types cannot be removed once order has been sent to TMW 
        
        selectedFuelTypeId = ApexPages.currentPage().getParameters().get('selectedFuelTypeId');   
        List<Fuel_Type__c> FuelTypesToBeRemoved = [SELECT Id, Fuel_Type__c FROM Fuel_Type__c
                                               WHERE Id = :selectedFuelTypeId];

        system.debug('FT Id ...........' + selectedFuelTypeId);
        system.debug('FT Control here ...........' + FuelTypesToBeRemoved);
        if(caseObj.SendtoTMW__c!=true)
            delete FuelTypesToBeRemoved;
        else
            result='CannotDelete';
        
        // Redirect back to same page
        PageReference reRend = new PageReference('/'+caseObj.Id);
        reRend.setRedirect(true);
        return reRend;
    } 
}



 
Best Answer chosen by Eric Blaxton 11
Eric Blaxton 11Eric Blaxton 11
Solved this on my own using sforce.one navigation.  Replace lines 63 thru 65
 
<div align="Center" draggable="false" >
                <apex:commandButton action="{!saveFuelType}" value="Add" reRender="" onclick="sforce.one.navigateToURL('/lightning/r/Case/{!caseObj.Case_Record_ID__c}/view')"/>
            </div>

 

All Answers

Eric Blaxton 11Eric Blaxton 11
Did more research...

it looks like Lightning converted my Visual Force page to an app. When the button is clicked i get this "https://x.lightning.force.com/one/one.app#(long alphanumeric)"
Eric Blaxton 11Eric Blaxton 11
Solved this on my own using sforce.one navigation.  Replace lines 63 thru 65
 
<div align="Center" draggable="false" >
                <apex:commandButton action="{!saveFuelType}" value="Add" reRender="" onclick="sforce.one.navigateToURL('/lightning/r/Case/{!caseObj.Case_Record_ID__c}/view')"/>
            </div>

 
This was selected as the best answer