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
Allada YeshwanthAllada Yeshwanth 

Unable to add child records to an existing parent record

public with sharing class Ctrl_myWrapper{

    public Id SaleBillId {get; set;}
    public list<ProductInvoiceWrapper> ProductsWrapped {get ; set; }
    public List<Product_Invoice__c> Like_Products { get; set; }
    public String Name { get; set; }
    Sale_Bill__c sb = new Sale_Bill__c();
    
    public Ctrl_myWrapper(ApexPages.StandardController controller) {
        
        Id SaleBillId = controller.getRecord().Id;
        Sale_Bill__c sb = [Select id,name from Sale_Bill__c where id = :SaleBillId Limit 1];
        ProductsWrapped = new List<ProductInvoiceWrapper>();
        Like_Products = new List<Product_Invoice__c>();
    }

    
    
    

    

    public PageReference searchProduct()
    {
        String likeName = '%'+Name+'%';
        Like_Products = [Select Id,Name,Product_Name__c,Quantity_Remaining__c,SellingPricePerUnit__c  From Product_Invoice__c where Product_Name__c Like :likeName];
        ProductsWrapped.clear();
        for(Product_Invoice__c p : Like_Products )
        {
            ProductsWrapped.add(new ProductInvoiceWrapper(p));
        }
        return null;
    }
    
    public PageReference selectedProducts()
    {
        List<Sale_Transaction__c> newSaleTransactions = new List<Sale_Transaction__c>();
        for(ProductInvoiceWrapper p : ProductsWrapped)
        {
            if(p.selected)
            {
                Sale_Transaction__c st = new Sale_Transaction__c();
                st.Bill_ID__c = sb.Id ;
                st.Product_Invoice__c = p.pinv.Id;
                st.Quantity__c = p.Quantity;
                newSaleTransactions.add(st);
            }
        }
        insert newSaleTransactions;
        return null;
    }

    public class ProductInvoiceWrapper{
    
        public Product_Invoice__c pinv {get; set;}
        public Boolean selected {get; set;}
        public Integer Quantity {get; set;}
        public ProductInvoiceWrapper(Product_Invoice__c a) 
        {
            this.pinv = a;
            selected = false;
            this.Quantity = 0;
        }
    
    
    }



}
Error : Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Bill_ID]: [Bill_ID]
Error is in expression '{!selectedProducts}' in component <apex:commandButton> in page my_wrapper_demo: Class.Ctrl_myWrapper.selectedProducts: line 49, column 1

Sale_Transaction__c is a junction object between Product__Invoice__c and Sale_bill__c

Please do help

Thanks in advance
Allada Yeshwanth 
 
Best Answer chosen by Allada Yeshwanth
VaasuVaasu
Remove Sale_Bill__c  from line 12, Instead use  sb = [Select id,name from Sale_Bill__c where id = :SaleBillId Limit 1];

All Answers

VaasuVaasu
Remove Sale_Bill__c  from line 12, Instead use  sb = [Select id,name from Sale_Bill__c where id = :SaleBillId Limit 1];
This was selected as the best answer
Allada YeshwanthAllada Yeshwanth
hi vaasu,

Your solution worked.

Thank you ... :)