How can I replace my oncomplete in the apex:commandButton to have a variable in it? Specifically, instead of going to the Account list I'm trying to get the whole page to reload for the record it's on (i.e. /lightning/r/Account/recordId
/view). This Visualforce Page is embedded within a tab on the Account object and it creating related Contact records. The standard window.top.location.reload() doesn't seem to be refreshing the entire page, just the VF form. window.top.location.replace() seems to work, but I'm failing in trying to get a variable in there. I think I'm missing something obvious.
Visualforce Page:
<apex:page controller="AddMultiplePets" showHeader="false" standardStylesheets="false" sidebar="false" docType="html-5.0" lightningStylesheets="true">
<head>
<apex:slds />
</head>
<style>
table tr:nth-child(4n-1), table tr:nth-child(4n) {background: #ccc;}
table tr {background: #eee;}
.fieldLabel {font-weight: 800; font-size:medium;}
body textarea, .slds-vf-scope textarea, body input[type="text"], .slds-vf-scope input[type="text"], body select, body select[size="0"], body select[size="1"], .slds-vf-scope select, .slds-vf-scope select[size="0"], .slds-vf-scope select[size="1"] {
border: 1px solid rgb(105 105 105);}
</style>
<div class="slds-scope">
<apex:form >
<apex:variable var="rowNum" value="{!0}" />
<apex:pageBlock >
<apex:variable var="rowNum" value="{!0}" />
<apex:pageBlockTable value="{!petList}" var="pet">
<apex:facet name="header">
<apex:commandLink action="{!insertRow}">
<button class="slds-button slds-button_brand">
<svg class="slds-button__icon slds-button__icon_left" aria-hidden="true">
<svg aria-hidden="true" class="slds-icon slds-icon-text-reverse">
<use xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:href="/apexpages/slds/latest/assets/icons/utility-sprite/svg/symbols.svg#add">
</use>
</svg>
</svg>
ADD ROW
<span class="slds-assistive-text">Add Row</span>
</button>
<apex:param value="{!rowNum}" name="index" />
</apex:commandLink>
</apex:facet>
<apex:column rowspan="2">
<apex:commandLink action="{!delRow}">
<button class="slds-button slds-button_destructive">
<svg class="slds-button__icon slds-button__icon_left" aria-hidden="true">
<svg aria-hidden="true" class="slds-icon slds-icon-text-reverse">
<use xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:href="/apexpages/slds/latest/assets/icons/action-sprite/svg/symbols.svg#delete">
</use>
</svg>
</svg>
DELETE: {!rowNum}
<span class="slds-assistive-text">Delete Row</span>
</button>
<apex:param value="{!rowNum}" name="index" />
</apex:commandLink>
<apex:variable var="rowNum" value="{!rowNum+1}"/>
</apex:column>
<apex:column >
<span class="fieldLabel">Species</span><br/>
<apex:inputField value="{!pet.Species__c}"/>
</apex:column>
//...bunch of other columns
<apex:column >
<span class="fieldLabel">Primary Living Area</span><br/>
<apex:inputField value="{!pet.Primary_Living_Area__c}"/>
</apex:column>
</apex:pageBlockTable>
<div style="text-align:center">
<apex:commandButton styleClass="slds-button slds-button_success" value="Create Records" action="{!insertPets}" oncomplete="window.top.location.replace('/lightning/o/Account/list?filterName=Recent)"/>
</div>
</apex:pageBlock>
</apex:form>
</div>
</apex:page>
APEX Class:
public class AddMultiplePets {
Id accId;
public List<Contact> petList {get;set;}
public Integer rowNum {get;set;}
public String lastNameString {get;set;}
public List<Contact> lastNameDefault {get; set;}
public AddMultiplePets(){
accId = ApexPages.currentPage().getParameters().get('Id');
petList = new List<Contact>();
petList.add(new Contact());
lastNameDefault = [SELECT LastName FROM Contact WHERE Id IN (SELECT npe01__One2OneContact__c FROM Account WHERE Id = :accId) LIMIT 1];
for (Contact currentLastName : lastNameDefault)
{lastNameString = currentLastName.LastName;}
}
public void insertPets(){
for(Contact objInt : petList){
objInt.AccountId = accId;
objInt.RecordTypeId = '0124P000000uNWFQA2';
objInt.LastName = lastNameString;
}
insert petList;
}
public void insertRow(){
petList.add(new Contact());
}
public void delRow(){
rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
petList.remove(rowNum);
}
}