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
Salesforce####Salesforce#### 

issue in Action function and javascript

i am giving the values in opportunity and when i click on the button "create aopportunity " it should create an opportunity record in the database and it should appear in the pageblock table which i am displaying . but some how it is not happening  can you please modify my code . 

vf page : 
<apex:page controller="JSexmpleOptyCont" id="page">
   <apex:form id="form">
     <script>
     alert("test1 success");
     function validateOpp()
     {
     alert("function inside  success");
     var optyName = document.getElementById('{!$Component.page.form.pgblck.pbsec.item1.optyname}').value;
     alert("optyName   success");
      var optyStage =   document.getElementById('{!$Component.page.form.pgblck.pbsec.item2.optyStage}').value;
    alert("optyStage success");
    var optyAccount = document.getElementById('{!$Component.page.form.pgblck.pbsec.item3.optyAccount}').value;
      alert("optyAccount success");
     var optyAmount= document.getElementById('{!$Component.page.form.pgblck.pbsec.item4.optyAmount}').value;
     alert("optyAmount success");
     
       alert ("optyAmount value "+optyAmount);
       
       
         
        if(optyAmount == null || optyAmount == '')
            {
                alert("amount cannot be empty");
            }
            else 
            {
            alert("no error");
            Createopp();
            }
     
        }
    
      </script>  
     

<apex:pageMessages id="msgs"/>
    
        <apex:sectionHeader title="Opportunity" subtitle="Opportunity List View"/>
   <apex:pageBlock mode="edit" id="pgblck">
                <apex:pageBlockButtons location="top">
                   <apex:commandButton value="Create Opportunity" onclick="validateOpp();" />
                   
                   <apex:actionFunction name="Createopp" id="Createopp" action="{!SaveRec}" /> 
                   <!--- <apex:actionStatus startText=" (incrementing...)"  id="counterStatus"/>-->
                </apex:pageBlockButtons>
        
        <apex:pageBlockSection title="Create New Opportunites" id="pbSec">
                
                <apex:pageBlockSectionItem id="item1">
                <apex:outputLabel value="Opportunity Name" for="Opportunity_Name"/>
                  <apex:inputField value="{!opty.name}" id="optyname"/>
                 </apex:pageBlockSectionItem>
             
              
                <apex:pageBlockSectionItem id="item2">
                <apex:outputLabel value="Stage" for="Stage"/>
                <apex:inputField value="{!opty.stagename}" id="optyStage"/>
                </apex:pageBlockSectionItem>
                
                <apex:pageBlockSectionItem id="item3">
                <apex:outputLabel value="Account" for="Account"/>
                <apex:inputField value="{!opty.AccountId}" id="optyAccount"/>
                </apex:pageBlockSectionItem>
                
                 <apex:pageBlockSectionItem id="item4">
                <apex:outputLabel value="Amount" for="Account"/>
                <apex:inputField value="{!opty.Amount}" id="optyAmount"/>
                </apex:pageBlockSectionItem>
         </apex:pageBlockSection>    
           
      
            <apex:pageBlockSection title="List of existing opportunities" columns="1">
                 <apex:pageBlockTable value="{!optylist}" var="opp" id="pbtable" >
                 <apex:column value="{!opp.Name}" />
                 <apex:column value="{!opp.AccountId }" />
                 <apex:column value="{!opp.stagename}" />
                 <apex:column value="{!opp.Amount}" />
                 
                 </apex:pageBlockTable>
           </apex:pageBlockSection>
           
   </apex:pageBlock>
</apex:form>
</apex:page>

========
controller : 
public with sharing class JSexmpleOptyCont 
{
public list<opportunity> optylist {set;get;}
public opportunity opty{set;get;}

public JSexmpleOptyCont()
{

optylist  = new list<opportunity>([select id ,AccountId ,Name,Amount , StageName from opportunity order by Name Asc]);
opty = new opportunity();
}

public PageReference SaveRec()
{  
    
    system.debug(opty+'here in controller');
    system.debug('========test2======'+opty);
    database.insert(opty,false );
    optylist  = new list<opportunity>([select id,Name,Type,Amount,StageName,AccountId  from opportunity  ]);
    Apexpages.addmessage(new apexpages.message(ApexPages.Severity.confirm,'Opportunity created succesfully'));
   // opty = null;
    
   return null; 



}
Best Answer chosen by Salesforce####
Dushyant SonwarDushyant Sonwar
Either reload the whole page after saving 
public PageReference saveRec(){
    try{
        insert opty;        
		Apexpages.addmessage(new apexpages.message(ApexPages.Severity.confirm,'Opportunity created succesfully'));
		return new pagereference('/yourpage').setRedirect(true);
	}
	catch(Exception ex){
	apexpages.message(ApexPages.Severity.Error,ex.getMessage());    
	   return null; 
	}
}


or rerender the pageblocktable "pbtable" (Currently that is not happening in code you are filling optylist but not rerendering the pageblock table)

<apex:actionFunction name="Createopp" id="Createopp" action="{!SaveRec}"  rerender="pbtable,msgs"/>