2

I am new in the Salesforce coding. I have two pages. On one page I am filling the data and saving it. After click on Save button, I want to see the exact same data which I filled on last page. This is my code.

When I click on Save button first time, it stay on the same page. When I click on Save button again, it throws a message "The page you submitted was invalid for your session. Please refresh your page and try again."

---------------------------------------FIRST VF PAGE-----------------------------------------

<apex:page controller="VisitorRegistration" showHeader="false" sidebar="false">
<apex:form >         
<apex:pageBlock title="Visitor Detail Page" mode="edit">      

    <apex:pageBlockSection title="Please fill the visitor information " collapsible="true">
      <apex:inputField value="{!visitor.Name}"/>  <br/>     
        <apex:pageMessages id="showmessage">
          </apex:pageMessages><br/>                     
            <apex:inputField value="{!visitor.CountryName__c}"/><br/>             
            <apex:inputField value="{!visitor.Passport_Number__c}"/><br/>           
            <apex:inputField value="{!visitor.Covid_19_Symtomp__c}"/><br/>
            <apex:inputField value="{!visitor.SendToQuarantineCenter__c}"/> <br/>           
            <apex:inputField value="{!visitor.CanVisitorGoToHome__c}"/><br/>
            <apex:inputField value="{!visitor.Quarantine_Cetner__c}"/><br/>
            <apex:inputField value="{!visitor.PatientEntryDate__c}"/><br/>
            <apex:inputField value="{!visitor.Patient_Exit_Date__c}"/><br/>
            </apex:pageBlockSection>        
                    <apex:pageBlockButtons location="top">
            <apex:commandButton action="{!saveV }" value="Save" reRender="true" />        
            </apex:pageBlockButtons>         
            </apex:pageBlock>
        </apex:form>    
</apex:page>

-----------------------------------------APEX CODE--------------------------------------------------

public class VisitorRegistration {   
   public Visitor__c visitor{get;set;}       
   public VisitorRegistration()
   {
       visitor= new Visitor__c();              
   }

   public void getVisitorID()
   {
       Id id = apexpages.currentpage().getparameters().get('Id');
   }   

   public PageReference saveV()
   {
       If(visitor.Name==null || visitor.Name=='')
       {
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter value'));
        }   

       INSERT visitor;     

       pagereference pr = new pagereference('/apex/VisitorRegistrationViewPage?id='+Visitor__c.id);  
        pr.setRedirect(false);
return pr;
    }
}

---------------------------------------SECOND VF PAGE----------------------------------------------------

<apex:page controller="VisitorRegistration" showHeader="false" sidebar="false">
    <apex:form >
        <apex:pageBlock title="Visitor Detail Page">

        <apex:pageBlockSection title="Visitor Information " collapsible="true">
            <apex:outputField value="{!visitor.Name}"/>  <br/>
            <apex:outputField value="{!visitor.CountryName__c}"/><br/>             
            <apex:outputField value="{!visitor.Passport_Number__c}"/><br/>           
            <apex:outputField value="{!visitor.Covid_19_Symtomp__c}"/><br/>
            <apex:outputField value="{!visitor.SendToQuarantineCenter__c}"/> <br/>           
            <apex:outputField value="{!visitor.CanVisitorGoToHome__c}"/><br/>
            <apex:outputField value="{!visitor.Quarantine_Cetner__c}"/><br/>
            <apex:outputField value="{!visitor.PatientEntryDate__c}"/><br/>
            <apex:outputField value="{!visitor.Patient_Exit_Date__c}"/><br/>
            </apex:pageBlockSection>
        </apex:pageBlock>    
         </apex:form>
</apex:page>
  • Not sure why you need 2 VF pages for this, am I right in assuming there is more to this problem than what you showed? – Sebastian Kessel Apr 30 at 21:48
  • Why? Why not use what Salesforce gives you out of the box? Are you planning to add some custom logic that can't be handled in triggers? – Sebastian Kessel Apr 30 at 21:51
  • Understood. My recommendation is to learn Aura or LWC and not VF, but i will assume there is a good reason for you to want to learn VF. – Sebastian Kessel Apr 30 at 21:56
3

First change your save code from

pagereference pr = new pagereference('/apex/VisitorRegistrationViewPage?id='+Visitor__c.id);  

to

pagereference pr = Page.VisitorRegistrationViewPage;
pr.getParameters().put('id',visitor.Id);

That way you're passing the Id of the record you just saved.

Also, you should change your construction to this (you do not need getVisitorId):

public VisitorRegistration()
{
   visitor= new Visitor__c();
   if (apexpages.currentpage().getparameters().containsKey('Id')) {
      Id visitorId = apexpages.currentpage().getparameters().containsKey('Id');
      List<Visitor__c> visitors = [SELECT <your fields Here> from Visitor__c WHERE Id = :visitorId LIMIT 1];       

      if (! visitors.isEmpty() {
        visitor = visitors[0];
      }
   }
}

NOTE: Since you said you were learning, I won't steer you towards using Standard Controllers, but you should research them for this specific use case.

| improve this answer | |

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.