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 

VF Controller save multiple child records against 1 master record

Hi,


I have a master object called Expense Form and a child object called Expense Item.  I trying to create a wizard which will allow me to save multiple expense item records against an expense form object.  With some help I have gotten this far, I have highlighted in red the part which I think is wrong.


With this code I am getting the following error:  Expression cannot be assigned at line -1 column -1.


Any help would be great.

 

 

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() {   
      List<Expense_Form__c> Eitems=new List<Expense_Form__c>();
      for (Expense_Form__c Eform : Eform)
      {
         Expense_Item__c Eitem=newEitems (Expense_Form__c.id=Eform.id);
         Eitems.add(Eitem);
      }
      insert Eform;
      }
    {
     PageReference EformPage = new ApexPages.StandardController(Eform).view();
            EformPage.setRedirect(TRUE);
   }
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

There's a couple of other issues here:

 

1. Eform is an instance of an Expense_Form__c, so iterating it doesn't make sense.

2. You are declaring eitems as a list of Expense_Form__c records, but then adding Expense_Item__c records to it.

 

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

All Answers

b-Forceb-Force

here is updated code

 

 

public PageReference save() {   
      List<Expense_Form__c> Eitems=new List<Expense_Form__c>();
      for (Expense_Form__c Ef : Eform)
      {
//I am expecting allready you have inserted Expense_Form__c and you have its Id 
         Expense_Item__c EI=new Expense_Item__c (Expense_Form__c.id=Ef.id);
         Eitems.add(EI);
      }
//Insert list of Expense_Item__c 
records
 insert Eitems; }

 

There was only issue while you create New object of Expense_Item__c

 

 

Hope it will help you

 

Thanks,

Bala

bob_buzzardbob_buzzard

There's a couple of other issues here:

 

1. Eform is an instance of an Expense_Form__c, so iterating it doesn't make sense.

2. You are declaring eitems as a list of Expense_Form__c records, but then adding Expense_Item__c records to it.

 

public PageReference save() {   
      List<Expense_Form__c> Eitems=new List<Expense_Form__c>();
      for (Expense_Form__c Eform : Eform)
      {
         Expense_Item__c Eitem=newEitems (Expense_Form__c.id=Eform.id);
         Eitems.add(Eitem);
      }
      insert Eform;
      }
This was selected as the best answer
Laytro80Laytro80

Thanks for all your help.