+ Start a Discussion
KyoKyo 

Create Autonumber with LineItem

I can not use the Field Autonumber Standard. because it was not Run 1,2,3,4. but it runs from Old LineItem  when creating New Lineitem.

 

Item API = Name

 

 

public class NewAddProducts{

    public List<Opportunities_Line_Item__c> accts {get; set;}
    
    public NewAddProducts(){
        accts = new List<Opportunities_Line_Item__c>();
        Opportunities_Line_Item__c opplineitem = new Opportunities_Line_Item__c();
        
        if(Apexpages.Currentpage().getParameters().get('ID')!=null) {
            opplineitem.OpportunitiesID__c = Apexpages.Currentpage().getParameters().get('ID');
            }
        
        //accts.add(new Opportunities_Line_Item__c());
        accts.add(opplineitem);
        
    }
    
    public void addrow(){
        Opportunities_Line_Item__c opplineitem = new Opportunities_Line_Item__c();
        
        if(Apexpages.Currentpage().getParameters().get('ID')!=null) {
            opplineitem.OpportunitiesID__c = Apexpages.Currentpage().getParameters().get('ID');
        }
         accts.add(opplineitem);
        //accts.add(new Opportunities_Line_Item__c());
    }
   public PageReference deleteRow() {
   if (accts.size()>1)
   {
      accts.remove(accts.size()-1);
   }
 return null;
 }
    public PageReference save(){
        insert accts;
        PageReference home = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
        home.setRedirect(true);
        return home;
    }
 public PageReference Cancel() {
        PageReference returnPage = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
        returnPage.setRedirect(true);
        return returnPage;
    }
    }

 

 

Thank you so much

bob_buzzardbob_buzzard

I'm afraid I can't see the question in the above post.  

 

Are you looking to create your own line item autonumber on this page?  Does the autonumber restart every time this page is visited (i.e. you could have multiple L1 records in the database)?

 

That being the case, could you not just set this when you create an element based on its position in the list?

 

E.g.

 

 

  public NewAddProducts(){
        accts = new List<Opportunities_Line_Item__c>();
        Integer idx=1;
        Opportunities_Line_Item__c opplineitem = new Opportunities_Line_Item__c(Name='L'+idx);
        idx++;
        
        if(Apexpages.Currentpage().getParameters().get('ID')!=null) {
            opplineitem.OpportunitiesID__c = Apexpages.Currentpage().getParameters().get('ID');
            }
        
        //accts.add(new Opportunities_Line_Item__c());
        accts.add(opplineitem);
        
    }

    public void addrow(){
        Integer idx=accts.size()+1;
     
        Opportunities_Line_Item__c opplineitem = new Opportunities_Line_Item__c(Name='L'+idx);
        
        if(Apexpages.Currentpage().getParameters().get('ID')!=null) {
            opplineitem.OpportunitiesID__c = Apexpages.Currentpage().getParameters().get('ID');
        }
         accts.add(opplineitem);
        //accts.add(new Opportunities_Line_Item__c());
    }

 

 

 

 

KyoKyo

Thank Bob, Your answer very well.

But I have with a small problem.

 

Ex. 

Frist AddProduct 3 Line

 

Add again

 


 

 

Thank you so much.

bob_buzzardbob_buzzard

I'd only expect this if your controller takes into account all of the existing line items and puts these in the list first.  Is your "Opportunities Line Item" a custom VF panel or similar?