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
acrozieracrozier 

creating adhoc object

I have created a form that our company uses for an incentive invoice. This form is generated off of a custom object that would retain the information. The invoice has a fluctuating number of line items and I would like users to be able to add/subtract these items as needed without having to create a bunch of unnecessary fields.  Is there any way to do this?

bob_buzzardbob_buzzard

If it were me, I'd look to model the line item as a separate custom object.  That way you can build a datatable (or similar repeater) based on a list of these objects.  

 

I wrote a blog post regarding managing a list of new objects - this would be straightforward to adapt to your requirement I think:

 

http://bobbuzzard.blogspot.com/2011/07/managing-list-of-new-records-in.html

acrozieracrozier

this is great and very helpful.  I am just having one problem which is probably my own stupidity.  How can I add that object as a related list to a custom object?  Or is this even possible?  If not, how can I put a button from my custom object to the page when the controller has to be set as ManageListController and not Job__c (my custom object)

acrozieracrozier

Ok, so I am getting somewhere, just need to learn how I can incorporate this.  Below the modified code to write to the AIIincentive object. 

 

public class ManageListController
{
 public List<AIIncentiveWrapper> wrappers {get; set;}
 public static Integer toDelIdent {get; set;}
 public static Integer addCount {get; set;}
 private Integer nextIdent=0;
  
 public ManageListController(ApexPages.StandardController controller)
 {
  wrappers=new List<AIIncentiveWrapper>();
  for (Integer idx=0; idx<5; idx++)
  {
   wrappers.add(new AIIncentiveWrapper(nextIdent++));
  }
 }
  
 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);
  }
 }
  
 public void addRows()
 {
  for (Integer idx=0; idx<addCount; idx++)
  {
   wrappers.add(new AIIncentiveWrapper(nextIdent++));
  }
 }
  
 public PageReference save()
 {
  List<AIIncentive__c> accs=new List<AIIncentive__c>();
  for (AIIncentiveWrapper wrap : wrappers)
  {
   accs.add(wrap.acc);
  }
   
  insert accs;
   
  return new PageReference('/' + Schema.getGlobalDescribe().get('AIIncentive__c').getDescribe().getKeyPrefix() + '/o');
 }
  
 public class AIIncentiveWrapper
 {
  public AIIncentive__c acc {get; private set;}
  public Integer ident {get; private set;}
   
  public AIIncentiveWrapper(Integer inIdent)
  {
   ident=inIdent;
   acc=new AIIncentive__c(Name='Incentives ' + ident);
  }
 }
}

 What I would like to do is have users enter these incentive amounts off of the Job__c object that we have.  I have a two part question on executing this.

 

1. how can I relate the Incentives to the Job they were created off of

 

2. I would like to display the incentive lines as either a related list or a button.  How can i accomplish either of these?

bob_buzzardbob_buzzard

Your incentives would need a lookup or master detail relationship to a job record.  That would add them as children when saving.  Once this relationship is in place, you can show them as a related list by adding the Incentives related list to the jobs page layout.