+ Start a Discussion
Semira@gmail.comSemira@gmail.com 

Cloning Line item does not save new changes after insert

Hi, I'm trying to figure out why does the line item do not save when I click on Quick Save. It should redict to edit page which it does, however it does not save the input value. I'm assuming it retrieving all the old date and inserting. However, how do I compare the line Item and see if there are new values and save it over?

Here's the code snippet below:
 
public void cloneLineItem(ID budgetId, ID oldBudget){

            System.debug('this is inside the clone method');
        // copy over the line items - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
         List<Budget_Line_Item__c> items = new List<Budget_Line_Item__c>();
         
         for (Budget_Line_Item__c LI : [Select Id, Name, Actual_Costs__c, Allocations__c, Budget__c, Burden__c, Estimated_Completion_Date__c, Fees__c, Gross_Profit__c, 
                                         In_House_Hours__c, In_House_Rate__c, In_House_Total__c, Materials__c, Profit__c, Revenue__c, Start_Date__c, Subcontractor__c, Subcontractor_bid__c, 
                                             Trade__c, Trade_Option__c FROM Budget_Line_Item__c WHERE Budget__c = :oldBudget]) {  
              Budget_Line_Item__c newLI = LI.clone(false);
              newLI.Budget__c = budgetId;
              items.add(newLI);

         }
         insert items;       

         BudgetRecord = (Budget__c)this.ExtCon.getRecord();  
    }
    
    public PageReference quickSaveBudget(){   
        
        PageReference pv;

       //ExtCon is the controller and saving the previous id for checking whether the record is a clone.

        ID OldBudgetID = ExtCon.getID();
        
        ExtCon.save();
        
        try{
 
            
            if(OldBudgetID != ExtCon.getID()){
            
                cloneLineItem(ExtCon.getID(), OldBudgetID);     
            }

              saveLineItem(ExtCon.getID());
              getBudgetLineItem();

            system.debug('What is the pv returning? '+ pv);
            
            
        }catch(Exception e){
            ApexPages.Message emsg = new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage());    
            ApexPages.addMessage(emsg);
        }        
        
        return pv;
    }
    
    public pageReference saveBudget(){
     
        PageReference pv;
        

        ID OldBudgetID = ExtCon.getID();
        ExtCon.save();
        
        try{
                   
            // Check if it is Save or Clone.
            // If CLone the execute clone line item
           // Then redirect to new budget ID.  
            
            if(OldBudgetID != ExtCon.getID()){
            
                cloneLineItem(ExtCon.getID(), OldBudgetID);
                      
            }                
                saveLineItem(ExtCon.getID());
                
            system.debug('What is the pv returning? '+ pv);
            
        } catch(exception e){
            ApexPages.Message ErrorMsg = new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage());
            ApexPages.addMessage(ErrorMsg);
        }
        
        pv = ExtCon.view();
       
        return pv;
    }
    
    public void saveLineItem(ID budgetID){ 
  
        List<Budget_Line_Item__c> saveBudgetLineItem = new List<Budget_Line_Item__c>();

        //try{
        for(WrapperClass wc:wrapBudget){
           if(wc.budget.Trade__c != null && wc.budget.Trade__c != ''){
                if(wc.budget.id==null){
                    wc.budget.Budget__c = budgetID;
                } 

                saveBudgetLineItem.add(wc.budget);
            } 

        } 
        
        upsert saveBudgetLineItem;
        if(delBudgetLineItem!=null){
            delete delBudgetLineItem;
        }
        
        BudgetRecord = (Budget__c)this.ExtCon.getRecord(); 

    }
     
    public class WrapperClass{
        public Integer indx{get;set;}
        public Budget_Line_Item__c budget{get;set;}    
        public WrapperClass(Integer indx,Budget_Line_Item__c budget){
            this.indx = indx;
            this.budget = budget;
        }
    
    }

 
SandhyaSandhya (Salesforce Developers) 
Hi,

You need to check if you are able to get the input value to your controller before your insert.

Thanks and Regards
Sandhya