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
mahesh p 54mahesh p 54 

After I click save button i need to display section

<apex:page Controller="CustomerController" >
    <apex:form >
        
        <apex:pageBlock title="Customer">

            <apex:pageBlockSection columns="2" title="Basic Information"> 
                <apex:inputField value="{!c.Name}"/>
                <br/>
                <apex:inputField value="{!c.Address_1__c}"/>
                <apex:inputField value="{!c.Address_2__c}"/>
                <apex:inputField value="{!c.Country__c}"/>
                <apex:inputField value="{!c.States__c}"/>
                <apex:inputField value="{!c.city__c}"/>
                <apex:inputField value="{!c.Zip_Code__c}"/>
                <apex:inputField value="{!c.Account__c}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="2" title="Tour Information"> 
                <apex:inputField value="{!c.Passport_Number__c}"/>                
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="2" title="Health Information"> 
                <apex:inputField value="{!c.Blood_Group__c}"/>                
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="1" title="Tour Packages"> 
            </apex:pageBlockSection>
            <apex:pageBlockTable value="{!listBookings}" var="b">
                <apex:column headerValue="Type a Destination">
                    <apex:inputField value="{!b.Type_a_destination__c}"/>
                </apex:column>
                <apex:column headerValue="Travel Mode">
                    <apex:inputField value="{!b.Travel_Mode__c}"/>
                </apex:column>
                <apex:column headerValue="From Date">
                    <apex:inputField value="{!b.From_Date__c}"/>
                </apex:column>
                <apex:column headerValue="To Date">
                    <apex:inputField value="{!b.To_Date__c}"/>
                </apex:column>
                <br/>
                <apex:column headerValue="Bookings for Customer">
                    <apex:inputField value="{!b.Customer__c}"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Add Multiple Bookings" action="{!addBookings}"/>
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
          <!--<apex:pageBlockTable value="{!samepage}" var="c">
          <!--<apex:column headerValue="Name">
          <apex:outputField value="{!c.name}"/>
          </apex:column>
          
          <apex:column headerValue="Address 1">
          <apex:outputField value="{!c.Address_1__c}"/>
          </apex:column>
          
          <apex:column headerValue="Address 2">
          <apex:outputField value="{!c.Address_2__c}"/>
          </apex:column>
          </apex:pageBlockTable>-->
        </apex:pageBlock>
        
    </apex:form>
</apex:page>
public with sharing class CustomerController {

    Booking__c b=new Booking__c();
    public list<Booking__c> listBookings{ get; set; }
    public Customer__c c { get; set; }

    public List<Customer__c> samepage { get; set; }
    
    public CustomerController(){
       c=new Customer__c();
        listBookings=new list<Booking__c>();
        listBookings.add(b);
    }

    Public void addBookings()
        {
            Booking__c b = new Booking__c();
            listBookings.add(b);
        }
    
    public PageReference save() {
       upsert c;  
       upsert b;
      samepage= [select id,Name,Address_1__c,Address_2__c,Country__c,States__c,city__c,Zip_Code__c,Account__c,Passport_Number__c,Blood_Group__c,Type_a_Destination__c,From_Date__c,To_Date__c from Customer__c where id=:c.id];
      
        return null;
    }
    
    public PageReference saveBookings() 
    {
        for(Integer i=0; i<listBookings.size(); i++)
            {
                insert listBookings;
            }
        upsert c;
        upsert b;
        return Page.Allbookingssaved;
    }
   }

Initially Tour Packages and Add Multiple Bookings should not be displayed.When i click save button the Tour Package section with Add Multiple Bookings needs to display.How can i achieve this?
Best Answer chosen by mahesh p 54
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi

Take a boolean variable and render the button, section using that boolean:

Class:
public with sharing class CustomerController {

    Booking__c b=new Booking__c();
    public list<Booking__c> listBookings{ get; set; }
    public Customer__c c { get; set; }

public Boolean Display{get;set;}

    public List<Customer__c> samepage { get; set; }
    
    public CustomerController(){
Display=false;
       c=new Customer__c();
        listBookings=new list<Booking__c>();
        listBookings.add(b);
    }

    Public void addBookings()
        {
            Booking__c b = new Booking__c();
            listBookings.add(b);
        }
    
    public PageReference save() {
Display=true;
       upsert c;  
       upsert b;
      samepage= [select id,Name,Address_1__c,Address_2__c,Country__c,States__c,city__c,Zip_Code__c,Account__c,Passport_Number__c,Blood_Group__c,Type_a_Destination__c,From_Date__c,To_Date__c from Customer__c where id=:c.id];
      
        return null;
    }
    
    public PageReference saveBookings() 
    {
        for(Integer i=0; i<listBookings.size(); i++)
            {
                insert listBookings;
            }
        upsert c;
        upsert b;
        return Page.Allbookingssaved;
    }
   }

and the rendered statment:
 
<apex:commandButton value="Add Multiple Bookings" action="{!addBookings}" rendered="{!Display}"/>
 
<apex:pageBlockSection columns="1" title="Tour Packages" rendered="{!Display}">

            </apex:pageBlockSection>

Hope this is useful.
Thanks.

All Answers

Bhargavi TunuguntlaBhargavi Tunuguntla
Hi

Take a boolean variable and render the button, section using that boolean:

Class:
public with sharing class CustomerController {

    Booking__c b=new Booking__c();
    public list<Booking__c> listBookings{ get; set; }
    public Customer__c c { get; set; }

public Boolean Display{get;set;}

    public List<Customer__c> samepage { get; set; }
    
    public CustomerController(){
Display=false;
       c=new Customer__c();
        listBookings=new list<Booking__c>();
        listBookings.add(b);
    }

    Public void addBookings()
        {
            Booking__c b = new Booking__c();
            listBookings.add(b);
        }
    
    public PageReference save() {
Display=true;
       upsert c;  
       upsert b;
      samepage= [select id,Name,Address_1__c,Address_2__c,Country__c,States__c,city__c,Zip_Code__c,Account__c,Passport_Number__c,Blood_Group__c,Type_a_Destination__c,From_Date__c,To_Date__c from Customer__c where id=:c.id];
      
        return null;
    }
    
    public PageReference saveBookings() 
    {
        for(Integer i=0; i<listBookings.size(); i++)
            {
                insert listBookings;
            }
        upsert c;
        upsert b;
        return Page.Allbookingssaved;
    }
   }

and the rendered statment:
 
<apex:commandButton value="Add Multiple Bookings" action="{!addBookings}" rendered="{!Display}"/>
 
<apex:pageBlockSection columns="1" title="Tour Packages" rendered="{!Display}">

            </apex:pageBlockSection>

Hope this is useful.
Thanks.
This was selected as the best answer
mahesh p 54mahesh p 54
wonderful could you please help me in resolving one more issue.When i click save customer is getting inserted into customers custom object and the section tour packages is displayed.when i click on add multiple bookings its fetching another row but that row is not getting inserted into bookings custom object.
Gulafsha MohammedGulafsha Mohammed
VF PAGE:
<apex:page Controller="CustomerController" >
    <apex:form >
        
        <apex:pageBlock title="Customer">

            <apex:pageBlockSection columns="2" title="Basic Information"> 
                <apex:inputField value="{!c.Name}"/>
                <br/>
                <apex:inputField value="{!c.Address_1__c}"/>
                <apex:inputField value="{!c.Address_2__c}"/>
                <apex:inputField value="{!c.Country__c}"/>
                <apex:inputField value="{!c.States__c}"/>
                <apex:inputField value="{!c.city__c}"/>
                <apex:inputField value="{!c.Zip_Code__c}"/>
                <apex:inputField value="{!c.Account__c}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="2" title="Tour Information"> 
                <apex:inputField value="{!c.Passport_Number__c}"/>                
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="2" title="Health Information"> 
                <apex:inputField value="{!c.Blood_Group__c}"/>                
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="1" title="Tour Packages"> 
            </apex:pageBlockSection>
            <apex:pageBlockTable value="{!listBookings}" var="b">
                <apex:column headerValue="Type a Destination">
                    <apex:inputField value="{!b.Type_a_destination__c}"/>
                </apex:column>
                <apex:column headerValue="Travel Mode">
                    <apex:inputField value="{!b.Travel_Mode__c}"/>
                </apex:column>
                <apex:column headerValue="From Date">
                    <apex:inputField value="{!b.From_Date__c}"/>
                </apex:column>
                <apex:column headerValue="To Date">
                    <apex:inputField value="{!b.To_Date__c}"/>
                </apex:column>
                <br/>
                <apex:column headerValue="Bookings for Customer">
                    <apex:inputField value="{!b.Customer__c}"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Add Multiple Bookings" action="{!addBookings}"/>
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
          <apex:pageBlockSection columns="1" title="Tour Packages" rendered="{!Display}">
          <apex:pageBlockTable value="{!samepage}" var="c">
         <apex:column headerValue="Name">
          <apex:outputField value="{!c.name}"/>
          </apex:column>
          
          <apex:column headerValue="Address 1">
          <apex:outputField value="{!c.Address_1__c}"/>
          </apex:column>
          
          <apex:column headerValue="Address 2">
          <apex:outputField value="{!c.Address_2__c}"/>
          </apex:column>
          </apex:pageBlockTable>
         </apex:pageBlockSection>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>
Controller:
 
public with sharing class CustomerController {

    Booking__c b=new Booking__c();
    public list<Booking__c> listBookings{ get; set; }
    public Customer__c c { get; set; }
    public Boolean Display{get;set;}

    public List<Customer__c> samepage { get; set; }
    
    public CustomerController(){
       Display=false;
       c=new Customer__c();
        listBookings=new list<Booking__c>();
        listBookings.add(b);
    }

    Public void addBookings()
        {
            Booking__c b = new Booking__c();
            listBookings.add(b);
        }
    
    public PageReference save() {
Display=true;
       upsert c;  
       upsert b;
      samepage= [select id,Name,Address_1__c,Address_2__c,Country__c,States__c,city__c,Zip_Code__c,Account__c,Passport_Number__c,Blood_Group__c,Type_a_Destination__c,From_Date__c,To_Date__c from Customer__c where id=:c.id];
      
        return null;
    }
    
    public PageReference saveBookings() 
    {
                insert listBookings;
        upsert c;
        upsert b;
        return Page.Allbookingssaved;
    }
   }
You need not run a loop to insert  list.

Please let me know if this helps.

Happy to help further.

Kindly mark this as solved if the reply was helpful.

Thanks,
Gulafsha
mahesh p 54mahesh p 54
No they are not getting inserted i removed the for loop.
Bhargavi TunuguntlaBhargavi Tunuguntla
Can I know after Click 'Add bookings' which method is invoked for saving the newly created records?

--- Clarification
mahesh p 54mahesh p 54
after clicking add multiple bookings it just need to fetch another row in VF page then i again click on save button which needs to perform insertion operation of booking records into bookings custom object
Bhargavi TunuguntlaBhargavi Tunuguntla
then place ' insert listBookings;' this statement in save method .
public PageReference save() {
Display=true;
insert listBookings;
       upsert c;  
       upsert b;
      samepage= [select id,Name,Address_1__c,Address_2__c,Country__c,States__c,city__c,Zip_Code__c,Account__c,Passport_Number__c,Blood_Group__c,Type_a_Destination__c,From_Date__c,To_Date__c from Customer__c where id=:c.id];
      
        return null;
    }
mahesh p 54mahesh p 54
Its showing this error:
Insert failed. First exception on row 0 with id a0B0K00000dJAOlUAO; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!save}' in component <apex:commandButton> in page customers: Class.CustomerController.save: line 26, column 1
 
Bhargavi TunuguntlaBhargavi Tunuguntla
try :
 
public PageReference save() {
Display=true;
upsert listBookings;
       upsert c;  
       upsert b;
      samepage= [select id,Name,Address_1__c,Address_2__c,Country__c,States__c,city__c,Zip_Code__c,Account__c,Passport_Number__c,Blood_Group__c,Type_a_Destination__c,From_Date__c,To_Date__c from Customer__c where id=:c.id];
      
        return null;
    }

 
mahesh p 54mahesh p 54
how to show message in an alert box once the save button is clicked saying the "customer is inserted"  before tour packages section is displayed and after tour packages is displayed show message saying "bookings are done"
Bhargavi TunuguntlaBhargavi Tunuguntla
Use:

<apex:pageMessages/> in VF Page.

and in Class:
public PageReference save() {

upsert listBookings;
upsert c; 
upsert b; 
samepage= [select id,Name,Address_1__c,Address_2__c,Country__c,States__c,city__c,Zip_Code__c,Account__c,Passport_Number__c,Blood_Group__c,Type_a_Destination__c,From_Date__c,To_Date__c from Customer__c where id=:c.id]; 
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));

Display=true; 
return null;
​ }


 
 
mahesh p 54mahesh p 54
For the first time when i click save button its great that i display 
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!')); this as my message.But after the save button is clicked the tour packages section is displayed and i am adding either a single booking or multiple bookings then i need to display something like this 
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Bookings are inserted Successfully.Thank you!'));
Is this Possible?
Thank you
Bhargavi TunuguntlaBhargavi Tunuguntla
public Integer i=0;
public integer i=0; //initialize in constructor
public PageReference save() {

upsert listBookings;
upsert c; 
upsert b; 
samepage= [select id,Name,Address_1__c,Address_2__c,Country__c,States__c,city__c,Zip_Code__c,Account__c,Passport_Number__c,Blood_Group__c,Type_a_Destination__c,From_Date__c,To_Date__c from Customer__c where id=:c.id]; 
if(i==0)
{
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
}
else
{
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Bookings are inserted Successfully.Thank you!'));
}
i=1;

Display=true; 
return null;
​ }