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
mw6mw6 

How to open a add new record page when a custom button is click

Hi,

I have a visualforce page  with a button 'RCA' and when I click this I need to open a add new record screen for an object 'RCA__c', is it possible, please help
Best Answer chosen by mw6
Ishan.AroraIshan.Arora

just pass a page refference of the create new record page of object u want to create record

copy the url and pass it in page refference

public PageReference newRecord() {
        
        //initilizing pagereference
        PageReference pageRef;
        
        
        //using try catch block for error handling
        try {

                        // add url of new record page of your object
                        String url='url of your new record page';
                        
                        // page reference for new page
                        pageRef  = new PageReference(url);

        } 
        catch(Exception e)
        {
            
            ApexPages.addMessages(e);
            
            pageRef =  new PageReference('/apex/TransactionController');
        }
        
        Return pageRef;
    }

All Answers

Ishan.AroraIshan.Arora

just pass a page refference of the create new record page of object u want to create record

copy the url and pass it in page refference

public PageReference newRecord() {
        
        //initilizing pagereference
        PageReference pageRef;
        
        
        //using try catch block for error handling
        try {

                        // add url of new record page of your object
                        String url='url of your new record page';
                        
                        // page reference for new page
                        pageRef  = new PageReference(url);

        } 
        catch(Exception e)
        {
            
            ApexPages.addMessages(e);
            
            pageRef =  new PageReference('/apex/TransactionController');
        }
        
        Return pageRef;
    }
This was selected as the best answer
Ishan.AroraIshan.Arora
call the method in button

 <apex:commandButton value="Login" action="{!newRecord}"/>
GulshanRajGulshanRaj
Hi,

Simply use this in you VF page:
<apex:commandButton action="{!URLFOR($Action.RCA__c.New)}" value="RCA"/>

Thanks
Gulshan Raj​