0

I have two section in which one is dynamically updated rows... I need top create cases using the first section and each row of the dynamic rows generated. I have used the following code but I am not able to save the records. Please suggest.

public class DynamicRows
{
    public list<Order__C> orderList{get;set;}
    public list<Orderwrapper> orderwrapperList{get;set;}
    public Order__C orderObj;
    public Integer counter{get;set;}

     public DynamicRows(ApexPages.StandardController controller)
     {
       String sql = 'SELECT Name, Product__c, Quantity__c, Packaging__c,Case__c, Order_Description__c, Exchanged_Through__c, Order_Cancelled__c   FROM Order__c';
        orderList = Database.Query(sql);

          orderObj = (Order__C)controller.getRecord();

         counter = 0;
           orderList= new list<Order__C>(); 
           orderwrapperList= new list<Orderwrapper>();
           for(Integer i=0;i<1;i++){
               Orderwrapper ordWrap = new Orderwrapper(new Order__C()); 
               counter++;
               ordWrap.counterWrap = counter;
               orderwrapperList.add(ordWrap); 

           }

     }
 /*    
   public void AddRow()
    {
        memberAddList.add(new Order__c());

    }  

    public void RemoveRow()
    {
      //  memberAddList.remove(new Order__c());
    }  
    */

      public PageReference addRow(){

        Orderwrapper ordWrap = new Orderwrapper(new Order__C()); 

        counter++;
        ordWrap.counterWrap = counter; 
        orderwrapperList.add(ordWrap); 
        return null;    
    }
    public PageReference removingRow(){

        Integer param = Integer.valueOf(Apexpages.currentpage().getParameters().get('index'));

        for(Integer i=0;i<orderwrapperList.size();i++){
            if(orderwrapperList[i].counterWrap == param ){
                orderwrapperList.remove(i);     
            }
        }

        counter--;
        return null;    
    }

    public PageReference saving(){
        list<Order__C> updateOrderList;
        updateOrderList= new list<Order__C>();
        if(!updateOrderList.isEmpty()){
            for(Orderwrapper orderWrapper:orderwrapperList){
                updateOrderList.add(orderWrapper.order);
            }
        }
        if(!updateOrderList.isEmpty()){
            upsert updateOrderList;
        }
       ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Info,'Record Saved Successfully.');
       ApexPages.addMessage(myMsg); 
       return (new ApexPages.StandardController(orderObj )).view();
    }

    public class Orderwrapper{
        public Order__C order{get;set;}
        public Integer counterWrap{get;set;}

        public Orderwrapper(Order__C ord){
            this.order = ord;  

        }
    } 

}




<apex:page standardController="Order__c" extensions="DynamicRows">
   <apex:form >
      <apex:pageBlock title="Orders">
            <apex:pageBlockSection title="Order Details">
                    <apex:inputField value="{!Order__c.Case__c}"/>
                    <apex:inputField value="{!Order__c.Name}"/>
                    <apex:inputField value="{!Order__c.Order_Description__c}"/>
                    <apex:inputField value="{!Order__c.Exchanged_Through__c}"/>
                    <apex:inputField value="{!Order__c.Order_Cancelled__c}"/>
            </apex:pageBlockSection>
             <apex:pageBlockSection title="Add Products">
              <apex:pageBlock title=" Products List ">
                <apex:pageMessages ></apex:pageMessages>
                <apex:pageBlockButtons location="top">
                    <apex:commandButton value="Add Row" action="{!addRow}" reRender="table" immediate="true"/>
                </apex:pageBlockButtons>
                <apex:pageBlockTable value="{!orderwrapperList}" var="page" id="table"> 
                    <apex:column headerValue="Product">
                        <apex:inputField value="{!page.order.Product__c}"/>
                    </apex:column>
                    <apex:column headerValue="Quantity">
                        <apex:inputField value="{!page.order.Quantity__c}" />
                    </apex:column>
                     <apex:column headerValue="Packaging">
                        <apex:inputField value="{!page.order.Packaging__c}" />
                    </apex:column>
                    <apex:column headerValue="Action">
                        <apex:commandLink value="Delete" action="{!removingRow}" immediate="true">
                            <apex:param name="index" value="{!page.counterWrap}"/>  
                        </apex:commandLink>
                    </apex:column>
                </apex:pageBlockTable>
                <apex:commandButton value="Save" action="{!saving}" />            
        </apex:pageBlock>      
        </apex:pageblockSection>        
        <apex:pageblockSection columns="1" >
            <apex:pageblockSectionItem >
                <apex:commandButton value="Save" />
                <apex:commandButton value="Cancel" />
            </apex:pageblockSectionItem>         
        </apex:pageblockSection>
       </apex:pageBlock>
    </apex:form>
</apex:page>

2 Answers 2

1

If your question is why saving doesn't work, then this might be the answer.

In the method 'saving', you create an empty list.

list<Order__C> updateOrderList;
updateOrderList= new list<Order__C>();

The problem is the part below. If the created list is NOT empty, then the orderWrappers will be added to the list to be insert/update later. But the updateOrderList is always empty, hence, nothing will be added to updateOrderList.

if(!updateOrderList.isEmpty()){
 for(Orderwrapper orderWrapper:orderwrapperList){
            updateOrderList.add(orderWrapper.order);
 }
}
if(!updateOrderList.isEmpty()){
    upsert updateOrderList;
}

I think you wanted to do this:

if(!orderwrapperList.isEmpty()){
 for(Orderwrapper orderWrapper:orderwrapperList){
            updateOrderList.add(orderWrapper.order);
 }
}
2

+1 to lw29's answer, but just want to make this point...

When writing code that loops there is no need to also have isEmpty checks because the loop just never gets entered anyway. And while older code examples have isEmpty checks before insert/update/upsert calls, that is not necessary either as a zero length argument results in the call to the database being skipped.

Less lines of code means less chance of introducing bugs:

list<Order__C> updateOrderList = new list<Order__C>();
for(Orderwrapper orderWrapper : orderwrapperList){
    updateOrderList.add(orderWrapper.order);
}
upsert updateOrderList;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .