+ Start a Discussion
Dave BerenatoDave Berenato 

PageReference to Reload Page not Working

I made an Apex Class to create a Note (we use a Custom Note Object Note__c)
 
public class NewNoteAccount {
    
    public id AccId;    
    public Account Acc{get;set;}
    public Note__c note {get;set;}
    public NewNoteAccount(ApexPages.StandardController stdController){

        this.Acc = (Account)stdController.getRecord();
        string AccId = Acc.id;
        
        Acc=[SELECT ID,Name FROM Account WHERE id=:AccId];
        
        note = new Note__c();
        note.Account__c = Acc.Id;

    }   
        public PageReference Create(){
        insert note;
        PageReference pageRef = new PageReference('/lightning/r/Account/' + AccId + '/view');
        pageRef.setRedirect(true);
        return pageRef;
		}

}

I put the Visualforce Page in a Lightning Action so I can create a Note from the Account:
 
<apex:page lightningStylesheets="true" standardController="Account" extensions="NewNoteAccount" showHeader="false">

<script type="text/javascript"> function CloseWindow()  {   window.close();  }
    window.onunload = refreshParent;
    function refreshParent() {
        window.opener.location.reload();
    }

 </script>


<apex:form >
    <apex:pageBlock title="New Note">
        <apex:pageBlockSection >
            <apex:inputfield value="{!Note.Name}" required="true"/>
            <apex:inputfield value="{!Note.Content__c}" required="true"/>
           <br/>
            <apex:outputfield value="{!Note.Account__c}" style="width:90%"/>
        </apex:pageBlockSection>
        <apex:commandButton value="Create" action="{!Create}" styleClass="slds-button slds-button_brand"  status="closer" oncomplete="CloseWindow();"/>
    </apex:pageBlock>
    </apex:form>    
</apex:page>

But when I click Create on the Lightning Action, it loads the URL:
 
https://corere--fair.lightning.force.com/lightning/r/Account/null/view

Why doesn't the URL have access to AccId?
Deepali KulshresthaDeepali Kulshrestha
Hi Dave,

I've gone through your code and you should do minor changes on your code:      

1. Replace your command button onComplete() with this:-->

  <apex:commandButton value="Create" action="{!Create}" styleClass="slds-button slds-button_brand" status="closer" oncomplete="window.location.top.reload();"/>

2.Replace the PageReference URL :

pagereference pageRef= new pagereference('/' + AccId);

pagereference pr = new pagereference('/' + a.id);


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com