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
abi1.2911902284080376E12abi1.2911902284080376E12 

Insert list of records from a wrapper class

I have created a visualforce page that displays a datatable with columns containing values  from various standard objects. I have used a wrapper class for this and populate it in  the datatable using a list of the type wrapper class. Now in the visualforce page i wanna save each row data to a custom object. How do i do this?  

In visual force page i use a standard controller contract and the extension as wrapper class.

bob_buzzardbob_buzzard

Are you wanting to pick up changes from the user?  If so, as long as your datatable is inside a form and you have inputfields backed by the various properties of the wrapper class, when the form is submitted, your wrapper class list will be updated with the user's input.  So you can just iterate the list and do what you need.

abi1.2911902284080376E12abi1.2911902284080376E12

I have to insert a list of records to a custom object called, "ContractLineitems__c" . This object has fields, whose values has to be retrieved from other standard objects , opportunity,contract,opportunity line item. So i have used wrapper class to display these values in visualforce page. Now i use a command button to save these values to the custom object, "ContractLineitems__c". How do i save ?? please suggest.

 

visual force page:

 

<apex:page standardController="Contract" sidebar="False" extensions="myControllerExtension">
    <apex:form > 
      
        <html>
            <apex:pageBlock >
            <table>
              
                <tr>  
                <apex:dataTable value="{!ContractItem1}" var="conItem"  border="1" width="100%" rowClasses="odd,even" styleClass="tableClass">
                <apex:column style="font-style:bold">
                <apex:facet name="header">Mobile Number</apex:facet>           
                <apex:InputText value="{!conItem.Mobile_Number}"/>
                </apex:column>
                <apex:column style="font-style:bold">
                <apex:facet name="header">Porting Mobile Number</apex:facet>           
                <apex:InputText value="{!conItem.Porting_Mobile_Number}"/>
                </apex:column>
                <apex:column style="font-style:bold">
                <apex:facet name="header">Type</apex:facet>           
                <apex:InputField value="{!contract.HD_PicklistRecType__c}"/>
                </apex:column>
                <apex:column style="font-style:bold">
                <apex:facet name="header">IMEI Number</apex:facet>           
                <apex:InputText value="{!conItem.IMEINumber}"/>
                </apex:column>
                <apex:column style="font-style:bold">
                <apex:facet name="header">SIM Number</apex:facet>           
                <apex:InputText value="{!conItem.SIMNumber}"/>
                </apex:column>
                <apex:column style="font-style:bold">
                <apex:facet name="header">Main Tariff</apex:facet>           
                <apex:OutputText value="{!conItem.MainTariff}"/>
                </apex:column>          
                <apex:column style="font-style:bold">
                <apex:facet name="header">Bolt1</apex:facet>           
                <apex:OutputText value="{!conItem.BoltOn1}"/>
                </apex:column>
                <apex:column style="font-style:bold">
                <apex:facet name="header">Bolt2</apex:facet>           
                <apex:OutputText value="{!conItem.BoltOn2}"/>
                </apex:column>
                <apex:column style="font-style:bold">
                <apex:facet name="header">Bolt3</apex:facet>           
                <apex:OutputText value="{!conItem.BoltOn3}"/>
                </apex:column>            
                <apex:column style="font-style:bold">
                <apex:facet name="header">Commission</apex:facet>           
                <apex:OutputText value="{!conItem.Commission}"/>
                </apex:column>
                <apex:column style="font-style:bold">
                <apex:facet name="header">Total Price(Inc Extra(s))</apex:facet>           
                <apex:OutputText value="{!conItem.Line_Total_Price}"/>
                </apex:column>
                <apex:column style="font-style:bold">
                <apex:facet name="header">Product Code</apex:facet>           
                <apex:OutputText value="{!conItem.productcode}"/>
                </apex:column>
                <apex:column style="font-style:bold">
                <apex:facet name="header">Product Name</apex:facet>           
                <apex:OutputText value="{!conItem.productname}"/>
                </apex:column>
                </apex:dataTable>   
                </tr>
                <tr>
                <td>
                <apex:dataTable value="{!total}" var="tot"  border="1" width="250px" rowClasses="odd,even" styleClass="tableClass" >
                <apex:column style="font-style:bold">
                Product Total
                </apex:column>           
                <apex:column >
                <apex:OutputText value="{!tot.productTotal}"/>
                </apex:column>
                <apex:column >
                <apex:OutputText value="{!tot.priceTotal}"/>
                </apex:column>
                 <apex:column >
                <apex:OutputText value="{!tot.commissionTotal}"/>
                </apex:column>                      
                </apex:dataTable>
                </td>
                <td>             
                <apex:pageBlockButtons >                                
                     <apex:commandButton value="Save" action="{!processSelected}"/> 
                </apex:pageBlockButtons> 
                    </td>             
                </tr>                 
            </table>
            </apex:pageBlock> 
        </html>                         
    </apex:form>
</apex:page>
abi1.2911902284080376E12abi1.2911902284080376E12
Below is the wrapper class i'v written
ontroller class : myControllerExtension
public class myControllerExtension 
{
    public Contract con;
    public List<CustomContract> queryResults;
    public List<GroupedQuery> queryTotal;
    Contract queries;
    decimal totalTprice;
    decimal commissionPrice;
    public myControllerExtension(ApexPages.StandardController stdController)
    {
        this.con = (Contract)stdController.getRecord();      
    }  
    public class CustomContract
    {       
        public string MainTariff{get;set;}
        public string Mobile_Number{get;set;}
        public string BoltOn1{get;set;}
        public string BoltOn2{get;set;}
        public string BoltOn3{get;set;}
        public string BoltOn4{get;set;}
        public string BoltOn5{get;set;}
        public string BoltOn6{get;set;}
        public decimal Commission{get;set;}
        public decimal Line_Total_Price{get;set;}
        public string ProductCode{get;set;}
        public string ProductName{get;set;}
        public string Porting_Mobile_Number{get;set;}
        public string SIMNumber{get;set;}
        public string IMEINumber{get;set;}               
        public Contract custContract{get;set;}     
        public CustomContract(Contract conObj)
        {
            custContract =  conObj;
        }
              
    }
    public class GroupedQuery
    {
        public integer productTotal{get;set;}
        public decimal priceTotal{get;set;}
        public decimal commissionTotal{get;set;}
    }
    CustomContract contractObj = new CustomContract(con);
   
    public List<CustomContract> getContractItem1()
    {   
        queryResults = new List<CustomContract>();
        totalTprice = 0;
        commissionPrice = 0;
        List<String> OppLineItem_Bolt = new List<String>();
        queries = [select opportunity__c , accountid , HD_Record_Type__c, id  from contract where id =: con.id];
        OpportunityLineItem[] oppLineItemsList = [Select id,PricebookEntry.Product2.Id,PricebookEntry.Product2.Name,Quantity,HD_DupTariff__c ,HD_DupDetailed__c,HD_Bolt_On__c,number__c,TotalPrice,PricebookEntry.Product2.ProductCode from OpportunityLineItem where OpportunityId =: queries.opportunity__c];      
        integer count = oppLineItemsList.size();
        for(OpportunityLineItem oppLItem : oppLineItemsList)
        {
            if(oppLItem.HD_Bolt_On__c)
                {                                                                             
                    //OppLineItem_Bolt1.add(i,oppLineItem.PricebookEntry.Product2.Name);      
                     OppLineItem_Bolt.add(oppLItem.PricebookEntry.Product2.Name);
                }
        }
        for(OpportunityLineItem oppLineItem : oppLineItemsList)
        {
            Decimal trim = oppLineItem.Quantity;
            Integer quantity = trim.intValue();
            System.debug('CHECK1'+ oppLineItem.PricebookEntry.Product2.Name);
 
            if(oppLineItem.HD_DupDetailed__c)
            {               
                For(Integer i=0 ; i < quantity; i++)
                {
                    if(queries != null)
                    {                               
                        CustomContract contractObj = new CustomContract(con);                                               
                        contractObj.Mobile_Number = '';
                        contractObj.Porting_Mobile_Number = '';
                        contractObj.SIMNumber = '';
                        contractObj.IMEINumber = '';                       
                        contractObj.Commission  = oppLineItem.number__c;                       
                        if(oppLineItem.number__c != null)
                        {
                            commissionPrice += oppLineItem.number__c;
                        }
                        contractObj.Line_Total_Price = oppLineItem.TotalPrice;
                         if(oppLineItem.number__c != null)
                        {
                            totalTprice += oppLineItem.TotalPrice;
                        }
                        contractObj.ProductCode = oppLineItem.PricebookEntry.Product2.ProductCode;
                        contractObj.ProductName = oppLineItem.PricebookEntry.Product2.Name;
                         
                        if(oppLineItem.HD_DupTariff__c)
                        {
                            contractObj.MainTariff = oppLineItem.PricebookEntry.Product2.Name;          
                        }
                        for(Integer j=0;j<OppLineItem_Bolt.size();j++)
                        {
                            System.debug('LISTITEM'+OppLineItem_Bolt);
                            if(OppLineItem_Bolt[j] != null)
                            {
                                if(j==0)                   
                                contractObj.BoltOn1 = OppLineItem_Bolt[j];
                                else if(j==1)
                                contractObj.BoltOn2 = OppLineItem_Bolt[j];
                                else if(j==2)
                                contractObj.BoltOn3 = OppLineItem_Bolt[j];                               
                            }
                        }
                        queryResults.add(contractObj);
                                                         
                    }
                }
            }             
        } 
        return queryResults;
    }
    public List<GroupedQuery> gettotal()
    {
        queryTotal = new List<GroupedQuery>();
        GroupedQuery obj = new GroupedQuery();
        Obj.productTotal = queryResults.size();
        Obj.priceTotal = totalTprice;
        Obj.commissionTotal = commissionPrice;
        queryTotal.add(obj);
        return queryTotal;
    }
    public PageReference processSelected()
    {
         upsert queryResults;
        return null;
    }
   
}

This upsert queryresults is not working. please suggest.