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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Visualforce Object and Wrapper Controller/Class Accessing record that is already inserted

Hi There,

I have a controller which is for an object, that has many children objects looking it up. The controller wraps the two children into a datatble. So far, everything on the page works, if I am inserting these records. I was wondering, what I would have to change within the controller, so that it can be used for editing records as well.

By editing, I essentially mean displaying the page, with all the inpput fields filled and the datatable full of child objects, how it looked just before save. I think i need to access the Fin record and all loan split and loan security records that look the Fin record up.

I am not sure how to phrase this, a point in the right direction would be much appreciated.

this is my controller:
 
public class FinanceNew{
    
    public Finance__c Fin { get; set; }
    public Finance_Loan_Security__c LoanSecurity { get; set; }
    public Finance_Loan_Split__c LoanSplit { get; set; }
    
    
    //Wrapper multi add try to implement
     public List<FinLoanSplitWrapper> wrappers {get; set;}
 public static Integer toDelIdent {get; set;}
 public static Integer addCount {get; set;}
 private Integer nextIdent=0;
 
 //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Wrapper 2 identical - 1
       public List<FinLoanSecurityWrapper> wrappers2 {get; set;}
 public static Integer toDelIdent2 {get; set;}
 public static Integer addCount2 {get; set;}
 private Integer nextIdent2=0;
 //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Wrapper 2 identical - 1
 
 ////////////////test
 
 public decimal AggregateLoanAmount{get;set;}
 public integer LoanSplitSize{get;set;}
 
 public void calculation()
{
AggregateLoanAmount = 0.00;
LoanSplitSize = 0;
  for (FinLoanSplitWrapper wrap : wrappers)
  {
  if(wrap.FinLoanS.Loan_Amount__c == null){
  wrap.FinLoanS.Loan_Amount__c = 0.00;
  }
   LoanSplitSize = LoanSplitSize + 1;
   AggregateLoanAmount = AggregateLoanAmount + wrap.FinLoanS.Loan_Amount__c;
  }
  

}

 

 
 ////////////////test
 
  
 public void delWrapper()
 {
  Integer toDelPos=-1;
  for (Integer idx=0; idx<wrappers.size(); idx++)
  {
   if (wrappers[idx].ident==toDelIdent)
   {
    toDelPos=idx;
   }
  }
   
  if (-1!=toDelPos)
  {
   wrappers.remove(toDelPos);
  }
 }
 
 //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Wrapper 2 identical - 2
  public void delWrapper2()
 {
  Integer toDelPos=-1;
  for (Integer idx=0; idx<wrappers2.size(); idx++)
  {
   if (wrappers2[idx].ident2==toDelIdent2)
   {
    toDelPos=idx;
   }
  }
   
  if (-1!=toDelPos)
  {
   wrappers2.remove(toDelPos);
  }
 }
 //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Wrapper 2 identical - 2
  
 public void addRows()
 {
  for (Integer idx=0; idx<addCount; idx++)
  {
   wrappers.add(new FinLoanSplitWrapper(nextIdent++));
  }
 }

  /* Remove for now, as it has been made redunant. keep for reference incase soemthing goes wrong
 public PageReference save()
 {
  List<Finance_Loan_Split__c> FLS =new List<Finance_Loan_Split__c>();
  for (FinLoanSplitWrapper wrap : wrappers)
  {
   FLS.add(wrap.FinLoanS);
  }
   
  insert FLS;
   
  return new PageReference('/' + Schema.getGlobalDescribe().get('Finance_Loan_Split__c').getDescribe().getKeyPrefix() + '/o');
 }
 */
  
 public class FinLoanSplitWrapper
 {
  public Finance_Loan_Split__c FinLoanS {get; private set;}
  public Integer ident {get; private set;}
   
  public FinLoanSplitWrapper(Integer inIdent)
  {
   ident=inIdent;
   FinLoanS=new Finance_Loan_Split__c(Loan_Split_Number__c=ident);
  }
 }
 
  //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Wrapper 2 identical - 3
 public void addRows2()
 {
  for (Integer idx=0; idx<addCount2; idx++)
  {
   wrappers2.add(new FinLoanSecurityWrapper(nextIdent2++));
  }
 }
 
 
/* Remove for now, keep for ref
 public PageReference save2()
 {
  List<Finance_Loan_Security__c> LoanSecurity =new List<Finance_Loan_Security__c>();
  for (FinLoanSecurityWrapper wrap : wrappers2)
  {
   LoanSecurity.add(wrap.FinSecS);
  }
   
  insert LoanSecurity;
   
  return new PageReference('/' + Schema.getGlobalDescribe().get('Finance_Loan_Split__c').getDescribe().getKeyPrefix() + '/o');
 }
*/
  
 public class FinLoanSecurityWrapper
 {
  public Finance_Loan_Security__c FinSecS {get; private set;}
  public Integer ident2 {get; private set;}
   
  public FinLoanSecurityWrapper(Integer inIdent)
  {
   ident2=inIdent;
   FinSecS=new Finance_Loan_Security__c(Loan_Security_Number__c=ident2);
  }
 }
  //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Wrapper 2 identical - 3
     //Wrapper multi add try to implement   

    
    
    
    

 public FinanceNew() {
    Fin = new Finance__c ();
    
     Fin.Finance_Office__c = ApexPages.currentPage().getParameters().get('OffId');
     Fin.Account__c = ApexPages.currentPage().getParameters().get('AccId');
    
    LoanSecurity = new Finance_Loan_Security__c ();
      wrappers=new List<FinLoanSplitWrapper>();
  for (Integer idx=0; idx<1; idx++)
  {
   wrappers.add(new FinLoanSplitWrapper(nextIdent++));
  }
      wrappers2=new List<FinLoanSecurityWrapper>();
  for (Integer idx=0; idx<1; idx++)
  {
   wrappers2.add(new FinLoanSecurityWrapper(nextIdent2++));
  }


      
    }
    
    
    public PageReference saveStandard() {
    
    Fin.Aggregate_Borrowings__c = AggregateLoanAmount;
    
    
     try {  
      upsert Fin;
        
        } catch (Exception e) {     
      
             ApexPages.addMessages(e);         
        }
        
 List<Finance_Loan_Split__c> FLS =new List<Finance_Loan_Split__c>();
  for (FinLoanSplitWrapper wrap : wrappers)
  {
   FLS.add(wrap.FinLoanS);
  }

 List<Finance_Loan_Security__c> LoanSecurity =new List<Finance_Loan_Security__c>();
  for (FinLoanSecurityWrapper wrap : wrappers2)
  {
   LoanSecurity.add(wrap.FinSecS);
  }
  
  for (Finance_Loan_Split__c FinLoanSplit: FLS)
  {
    FinLoanSplit.Finance__c = Fin.id;
   FinLoanSplit.Account__c = Fin.Account__c;
   FinLoanSplit.Office__c = Fin.Finance_Office__c;
  }
  
    for (Finance_Loan_Security__c LoanSec: LoanSecurity)
  {
    LoanSec.Finance__c = Fin.id;
   LoanSec.Account__c = Fin.Account__c;
   LoanSec.Office__c = Fin.Finance_Office__c;
  }            
        
        try { 
        
        upsert FLS;
        upsert LoanSecurity;
       
       
        PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+fin.Account__c+'&Sfdc.override=1');
        return pageRef;
        } catch (Exception e) {     
      
             ApexPages.addMessages(e);         
        }
        
        
        
        return null;
    
    }
    
      
    public PageReference Cancel() {
   
        PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+Fin.account__c+'&Sfdc.override=1');
        return pageRef;
     
    }

 
 
    
}

thank you in advance!
Nirmal ChristopherNirmal Christopher
I understand you are  trying to add an edit feature in  your visualforce page. I imagine you are have a detail page and while clicking Edit on this page it should redirect you to another visualforce page with the Input fields. You can use the same Apex class but you need to retrieve the records for edit functionality before upserting. Use the record Id from the URL and retreive the recods and populate the record value in the input field on your edit page and then add a save button in the Edit visual force page to upsert the records. 

Hope I answered your question. Hit best answer button if its helpful.

Regards,
Nirmal
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Thank you for your reply. Would you possibly be able to provide some example code on how I may achieve this. Thank you for your time.
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
What I am trying to do is to access the Fin record and all the related Loan secuirty and Loan split records, so than I can essentially use the exact same page to edit as I use to insert.