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
its_rajits_raj 

Multiple action for a single custom button.

Is it possible to perform multiple actions from a single custom button click? likewise, onclick of a button, the data gets saved as well as the page redirects to another link/page.

PrakashbPrakashb

Yes you can perform multiple action on a single button click as you have mentioned.

 

 

its_rajits_raj

HI Prakashb,
 Can u help with the syntax.

PrakashbPrakashb

You can use the below code to make an update and redirect toa different page.

 

Public Pagereference saveandredirect(){

 

Update yourobject;

Pagereference p = new Pagereference('your url');

p.setredirect(true);

return p;

}

its_rajits_raj

Visualforce Error

 


System.NullPointerException: Attempt to de-reference a null object

Error is in expression '{!saveandredirect}' in component <apex:page> in page detailpage

 

 

Class.myProductController.saveandredirect: line 28, column 1

 

got the above error 

 

PrakashbPrakashb

Can you post the entire code in the method??

its_rajits_raj

<apex:commandButton action="{!saveandredirect}" value="BUY NOW" />

 

public with sharing class myProductController {

    public myProductController(ApexPages.standardController controller) {
   
    String id = ((Product__c)controller.getRecord()).id;
       this.product  = [Select p.Name, p.Company__c, p.Model__c,p.Color__c,p.Price__c from Product__c p where p.id=:id];

    }
   
    public PageReference view() {
        return null;
    }

Product__c product;
Booking__c booking;
   

    public Product__c getProduct() {
        return product;
    }

   

    Public Pagereference saveandredirect(){

 

Update booking;

Pagereference p = new Pagereference('/apex/logipage');

p.setredirect(true);

return p;

}

   public PageReference gotologinpage() {
    return Page.loginpage;
    }
   }

PrakashbPrakashb

Your Booking record is null and hence when you are trying to update it it is throwing a null pointer exception.

 

Do you really need to update booking object as there seems to be no record related to it??

 

its_rajits_raj

i need to store the mentioned fields in a new object named as booking, upon save.

so the object will be null in the beginning and upon save should re direct to a new page.

 

i also used "insert" instead of "update" but still the error persists.

PrakashbPrakashb

Are you using the booking fields on your page.

 

It seems the product obejct is the controller for your page.

 

Any way try replacing Booking__c booking with Booking__c booking = new Booking__c();

 

its_rajits_raj

Hey Thanks. The record can now be created and even page navigation is working.

But 1 problem persists even now, New Booking record is created everytime but doesnot store the fields' values.
I m trying to store the fields' values from my Product object into the Booking Object.

PrakashbPrakashb

Then you need to map your Booking and Product fields before doing an insert.

 

Like booking.Fieldname  = product.Fieldname;

Vishal GuptaVishal Gupta

booking.Field = product.Field;

use this wn u click the button and then update the record....u'll get the fields stored
u need to think that wn u r creating a new record then u hv to map the fields also for storing the values.