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
Laytro80Laytro80 

Wizard quick query

I have built a wizard, it goes through various steps and creates 3 records across 3 objects all linked.

 

I now want to create more than one type of record on the same object and again link, but when I do only one record is created.

 

Is there anything obvious I should be doing.

 

Thanks

 

Ross

bob_buzzardbob_buzzard

Its very difficult to suggest anything without sight of the code.

Laytro80Laytro80

Thanks for the post, and sorry for not posting the code, it's below.  As you can see it's very basic.  There is a also a very basic visualforce page.

 

The booking object is easy. The customer object is basically the same as contact. The traveller object is really a junction which allows me to have a lookup (1 to many) to both customer and booking.

 

The wizard creates 1 booking, customer and traveller records.  But I need it to be able to create many customer and traveller. And to be honest not sure how to start.  Any advice would be great.

 

Thanks

 

Ross

 

 

public class newBookingManagerController {

    public newBookingManagerController(ApexPages.StandardController controller) {

    }

    
   Booking__c booking;   
   Customer__c customer;
   Traveller__c traveller;
    
   public Booking__c getBooking() {
      if(booking == null) booking = new Booking__c();
      return booking;
   }
   
   public Customer__c getcustomer() {
     if(customer == null) customer = new Customer__c();
     return customer;
   }

   public Traveller__c gettraveller() {
      if(traveller == null) traveller = new Traveller__c();
      return traveller;
   } 
    
    public PageReference cancel() {
            PageReference bookingPage = new ApexPages.StandardController(booking).view();
            bookingPage.setRedirect(true);
            return bookingPage; 
    }
    
   public PageReference save() {   
     
      insert booking;       
      insert customer; 
      traveller.booking__c = booking.id;
      traveller.customer__c = customer.id;
      traveller.CurrencyIsoCode = booking.CurrencyIsoCode;
      traveller.primary_customer__c = TRUE;
      insert traveller;

      PageReference BookingPage = new ApexPages.StandardController(Booking).view();
      BookingPage.setRedirect(true);

      return BookingPage;
   }

}

 

 

 

 

bob_buzzardbob_buzzard

Do you want to create these all in the save method?  E.g. are a bunch of different types to create at the same time or similar?

Laytro80Laytro80

Thanks for the reply.

 

This is a good example you might have one booking for one person.  So one booking, traveller and customer record.

 

You could have a booking with 3 people so 1 booking 3 customer and 3 traveller records.

 

I need them all created and saved within the same action.

 

Right now I have a simple solution which is you create a booking with one person and the booking, traveller and custom records are created and linked.  You are then returned to the booking page and you simply add the extra travellers.

 

It would be nice to have this all in one Wizard.

 

 

bob_buzzardbob_buzzard

So at the moment can you capture the three people into your booking form?

 

If you can (for example) capture them into a list, you could simply iterate that to create the objects like so:

 

 

  public PageReference save() {   
     
      insert booking;  
      List<Traveller__c> travellers=new List<Traveller__c>();
      insert customers; 
      for (Customer__c customer : customers)
      {
         Traveller__c traveller=new Traveller(Booking__c=booking.id,
                                              Customer__c=customer.id,
                                              CurrencyIsoCode=booking.CurrencyIsoCode,
                                              Primary_Customer__c=TRUE);
         travellers.add(traveller);
      }
      insert travellers;

      PageReference BookingPage = new ApexPages.StandardController(Booking).view();
      BookingPage.setRedirect(true);

      return BookingPage;
   }

 

 

Laytro80Laytro80

Hi,

 

Thanks so much for sample code and kind of understand the principle.

 

At the moment no, I can only create one booking, one customer, one traveller.

 

I want to be able to create many.

 

I have tried to create more but it's clear to me that the code I have is wrong.

 

I will use the code you sent to try and build a solution.

 

Thanks again for taking the time.

Laytro80Laytro80

Thanks again for your help.  Just wanted to ask another question, very similar.

 

I have an expense form object (master) and an expense item object (child).  Idea is you create the form and then, via the related list add new expense items.  I want to simplify the data entry process using a wizard which would auto populate some of the fields on the child using the master.

 

So there will be one expense form and many expense items.  The controller below was based on the code you kindly provided but I am going wrong.

 

All I am looking for is to be able to create the form and then as many items as need using visualforce and this controller.  Any tips would be good.

 

 

public class newExpensesController {
    
   Expense_Form__c Eform;   
   Expense_Item__c Eitem;
    
   public Expense_Form__c getEform() {
      if(Eform == null) Eform = new Expense_Form__c();
      return Eform;
   }
   
   public Expense_Item__c getEitem() {
     if(Eitem == null) Eitem = new Expense_Item__c();
     return Eitem;
   } 
    
    public PageReference cancel() {
            PageReference EformPage = new ApexPages.StandardController(Eform).view();
            EformPage.setRedirect(true);
            return EformPage; 
    }
    
    public PageReference save() {   
     
      insert Eform;     
      List<Expense_Item__c> Eitems=new List<Expense_Item__c>();
      for (Expense_Form__c Eform : Eforms)
      {
         Expense_Item__c Eitem=new Expense_Item__c (Expense_Form__c=Eform.id,
                                              CurrencyIsoCode=Eform.CurrencyIsoCode
                                              );
         Eitems.add(Eitem);
      }
      insert Eitems;

     PageReference EformPage = new ApexPages.StandardController(Eform).view();
            EformPage.setRedirect(true);
            return EformPage;
   }

}

 

 

bob_buzzardbob_buzzard

It looks like the right sort of thing - can you explain a bit about how you are going wrong?

Laytro80Laytro80

Yeah I just can't see were I am going wrong, the problem is with the save page reference.  I keep getting errors like 

 

 

Variable does not exist: Eforms at line 26 column 36.

 

 

public PageReference save() {   
     
      insert Eform;     
      List<Expense_Item__c> Eitems=new List<Expense_Item__c>();
      for (Expense_Form__c Eform : Eforms)
      {
         Expense_Item__c Eitem=new Expense_Item__c (Expense_Form__c=Eform.id,
                                              CurrencyIsoCode=Eform.CurrencyIsoCode
                                              );
         Eitems.add(Eitem);
      }
      insert Eitems;

     PageReference EformPage = new ApexPages.StandardController(Eform).view();
            EformPage.setRedirect(true);
            return EformPage;
   }

}

 

 

 

bob_buzzardbob_buzzard

You are getting this error because you don't have a property on the controller called eforms.  You have one called eform, but that is a single instance rather than a list, so you can't iterate it.

 

If I read correctly, this is a user filling in an expenses form with expenses items.  That being the case, presumably you should be iterating the eitems added to the page and creating a new instance of Expense_Item__c for each of those.