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
shakila Gshakila G 

How to avoid VF page Refresh before saving the record ?

Hi All,
I have a detail page Button(VF) to create a Components records for the parent record in standard Page.

After entering a input in VF page, If I press the Enter Key button before saving the record...automatically it's redirecting to the parent record page.

Due to this, I Lost my All Input values.Kindly Assist me to overcome this issue.

My VF page :

<apex:page standardController="Product2" extensions="AddProductComponent">


    <apex:form >
    
        

        <apex:pageMessages id="err"></apex:pageMessages>
      
       <apex:pageBlock title="Product Informations">
          
       <div align ="Right"> <apex:commandButton value="Back" action="{!Cancel}" /> </div>
        <table width="80%">
        <tr>
        <td>Product name</td>
        <td>{!Product2.name}</td>
        </tr>
        <tr>
        <td>Product Code</td>
        <td>{!Product2.ProductCode}</td>
        </tr>
        <tr>
        <td>Product Category</td>
        <td>{!Product2.Family}</td>
        </tr>
        </table>
   
        <apex:pageblockButtons location="bottom" >
        <apex:inputtext value="{!addCount}"/>
        <apex:commandButton value="Add Row" action="{!addRows}" rendered="{!show1}"/>
        
        </apex:pageblockButtons>
          <apex:pageblockButtons location="bottom">
        <apex:commandButton value="Save" action="{!save}"/>
        <apex:commandButton value="Cancel" action="{!Cancel}"/>
        </apex:pageblockButtons>
        
        </apex:pageBlock>
          
        <apex:pageBlock title="Component Informations" rendered="{!show1}" >  
        <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
        <apex:column headerValue="Ident">
        <apex:outputText value="{!wrapper.ident}"/>
        </apex:column>
        
      <apex:column headerValue="Raw Material Name">
        <apex:inputField value="{!wrapper.bkmas.Raw_Material__c}">    
        </apex:inputField>        
         </apex:column> 
      
        
        <apex:column headerValue="Measurement">
        <apex:inputfield value="{!wrapper.bkmas.Measurement__c}"/>
        </apex:column>
        
        <apex:column headerValue="Quantity">
        <apex:inputfield value="{!wrapper.bkmas.Quantity__c}"/>
        </apex:column>
        
        <apex:column headerValue="Description">
        <apex:inputfield value="{!wrapper.bkmas.Description__c}"/>
        </apex:column>
        </apex:pageBlockTable>
        
      
        </apex:pageBlock>
        

    </apex:form>
</apex:page>

Apex Class:

public class AddProductComponent {

   //Declarartions
    List<Product2> LstProduct = new List<Product2>();
    List<Component__c> lstComp = new List<Component__c>(); 
    String RecId;
    
    public static Integer addCount {get; set;}
    private Integer nextIdent=01;
    List<Innerclass> wrappers = new  List<Innerclass>();
    boolean show = true;
    boolean show1 = true;
    
    public AddProductComponent(ApexPages.StandardController controller) {
        RecId = ApexPages.currentPage().getParameters().get('id');
        LstProduct = [Select Id,Name,ProductCode,Family,Total_No_of_Raw_Materials__c
                      from Product2 where id=:RecId];
        lstComp  = [select id,Name,Raw_Material__c,Quantity__c,Measure_Unit__c,Description__c, 
                    Product__c ,Measurement__c,Raw_Material__r.Code__c, RawMaterial_Code__c from Component__c where Product__c =: RecId];
        
        if(lstComp.size()<=0){
            addRows();
        }
        else {
            show1 = false;
            Message();
        }            
        
    }
    
    public boolean getshow()
    {
         return show;
    }
    
    public boolean getshow1()
    {
         return show1;
    }
    
    public List<Innerclass> getwrappers()
    {
    return wrappers;
    }
   
    Public Void Message(){
       
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'You cannot enter Components to this Product, Please use Edit Component Button'));
    }
    
    public void addRows()
    {   
        for (Integer idx=0; idx<addCount; idx++)
        {
        wrappers.add(new Innerclass(nextIdent++));
        }
    }
    
    
    public pagereference save()
    {
        List <Component__c>  lstcomp =  new List <Component__c>();
        for(Innerclass wrap:wrappers)
        {
            Component__c component =  new Component__c();
            string ident = string.valueof(wrap.ident);
            if(ident.length()== 1) {
                 component.Name = '0'+ ident;
            } else{
                component.Name = ident;
            }
            component.Raw_Material__c = wrap.bkmas.Raw_Material__c;
            component.Quantity__c = wrap.bkmas.Quantity__c;
            component.Description__c = wrap.bkmas.Description__c;
            component.RawMaterial_Code__c = wrap.bkmas.RawMaterial_Code__c;
            component.Measurement__c = wrap.bkmas.Measurement__c;
            component.Product__c = Recid;
            lstcomp.add(component);
        }
        try {
            insert lstcomp;
            PageReference acctPage = new PageReference ('/' + RecId);
            acctPage.setRedirect(true);
            return acctPage;   
        } catch (exception ex) {
            apexpages.message err = new apexpages.message(apexpages.severity.ERROR,ex.getmessage());
            apexpages.addmessage(err);
            return null;
        }
    }
    
    
   //Intermediate Class()
    public class Innerclass
    {
    public string CustomId;
    public Component__c bkmas {get; private set;}
    public Integer ident {get; private set;}
        public Innerclass(Integer inIdent)
        {

        ident=inIdent;
        bkmas =new Component__c ();
        }
    }
}

 

SandhyaSandhya (Salesforce Developers) 
Hi,

You may try to use javascript function which intercepts key presses on your form elements.

Refer below document and try to implement according to your requirement.

https://developer.secure.force.com/cookbook/recipe/submit-a-form-with-the-enter-key
 
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya