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
Aladdin USAladdin US 

when click on save it should insert the record and also navigate to other page

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="LeadInsertController"> 
  <apex:form >
        <apex:pageBlock title="Lead Edit">
            <apex:pageBlockButtons >
                <apex:commandLink id="cl" action="{!saveit}" target="blank" >
                    <apex:commandButton value="Save" oncomplete="this.form.reset();return false;"/>
                </apex:commandLink>
            </apex:pageBlockButtons>
            <apex:pageMessages />
            <apex:pageblockSection title="Lead Information">
                <apex:inputField value="{!le.firstName}"/>
                <apex:inputField value="{!le.mobilePhone}"/>
                <apex:inputField value="{!le.lastName}"/>
                <apex:inputField value="{!le.email}"/>
                <apex:inputField value="{!le.company}"/>
                <apex:inputField value="{!le.status}"/>
            </apex:pageblockSection>
            <apex:pageblockSection columns="1" title="Address Information">
                <apex:inputField value="{!le.street}"/>
                <apex:inputField value="{!le.city}"/>
                <apex:inputField value="{!le.state}"/>
                <apex:inputField value="{!le.postalCode}"/>
                <apex:inputField value="{!le.country}"/>
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class LeadInsertController{
    
    public Lead le {get;set;}
    
    public LeadInsertController(){
        le = new Lead();
    }
    
    public PageReference saveit () {
        try {  
            insert le;
            le = new Lead();
        }
        catch (Exception e) {
            ApexPages.addMessages (e);
        }
        return page.SecondPage_Vf; // NAme of your other Visualforce Page
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Aladdin USAladdin US
Hi Anas,

                 Code is working but when click on save it should insert the record and at the same time should navigate to other vf page from previous vf page.

Thanks.
 
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Try this:

Visualforce:
<apex:page controller="LeadInsertController"> 
    <apex:form >
        <apex:pageBlock title="Lead Edit">
            <apex:pageBlockButtons >
                
                <apex:commandButton value="Save" id="cl" action="{!saveit}"/>
                
            </apex:pageBlockButtons>
            <apex:pageMessages />
            <apex:pageblockSection title="Lead Information">
                <apex:inputField value="{!le.firstName}"/>
                <apex:inputField value="{!le.mobilePhone}"/>
                <apex:inputField value="{!le.lastName}"/>
                <apex:inputField value="{!le.email}"/>
                <apex:inputField value="{!le.company}"/>
                <apex:inputField value="{!le.status}"/>
            </apex:pageblockSection>
            <apex:pageblockSection columns="1" title="Address Information">
                <apex:inputField value="{!le.street}"/>
                <apex:inputField value="{!le.city}"/>
                <apex:inputField value="{!le.state}"/>
                <apex:inputField value="{!le.postalCode}"/>
                <apex:inputField value="{!le.country}"/>
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class LeadInsertController{
    
    public Lead le {get;set;}
    
    public LeadInsertController(){
        le = new Lead();
    }
    
    public PageReference saveit () {
        try {  
            insert le;
            le = new Lead();
        }
        catch (Exception e) {
            ApexPages.addMessages (e);
        }
        return page.SecondPage_Vf; // Name of your other Visualforce Page
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Aladdin USAladdin US
Hi Anas,

Not navigating to other page. My another requirement is i have a button, "Submit for Approval" and my discount limit is 10. This button should get hide if discount is less than 10 and visible only if greater than 10