+ Start a Discussion
Sudhir_MeruSudhir_Meru 

Save Data from VisualForce Page to Custom Object

Hi, 
 
 I created a visual force page using below code. 

<apex:page title="Renewal Quote" controller="Renwal_Account_Contract" showHeader="false" sidebar="false" readOnly="true" cache="false">
<apex:form >
<apex:pageblock Title="Contract Based" rendered="{!ShowContractSection}"> 
  <apex:pageblockTable value="{!Asset_yourObjList }" var="Asset_obj">
  <apex:column headerValue="Check">
     <apex:inputCheckbox value="{!Asset_obj.Id}"/>
   </apex:column>  
    <apex:column value="{!Asset_obj.AccountId}"/>
    <apex:column value="{!Asset_obj.Product2Id}"/>
    <apex:column value="{!Asset_obj.SerialNumber}"/>
    <apex:column value="{!Asset_obj.last_contract_number__c}"/>
    <apex:column value="{!Asset_obj.Service_Start_Date_Min__c}"/>
    <apex:column value="{!Asset_obj.Service_End_Date_Max__c   }"/>
  </apex:pageblockTable>
</apex:pageblock>
</apex:form>
</apex:page>

For above visualforce page below is the code for controller.

public with sharing class Renwal_Account_Contract
{
   Id conId = ApexPages.currentPage().getParameters().get('Id');
    
  public Renwal_Account_Contract()
  { 
   GetAccount = new Asset();    //Get Account Id from Page
   GetContract = new Asset();   //Get Contract Name from Page
   total_size = [select count() from Asset ];  
   Filter_Asset_Records();  
   
  }
 
  public Asset GetAccount{get;set;}
  public Asset GetContract{get;set;}
   
  public List<Asset> Account_yourObjList{get;set;}
  public List<Asset> Asset_yourObjList{get;set;}  
  public List<Asset> Default_Asset_yourObjList{get;set;}

 
  //Function to Return Asset Report Passing Contract Name
  public void Filter_Asset_Records(){ 
     
    Asset_yourObjList = new List<Asset>();
    Asset_yourObjList = [SELECT Id,AccountId,Product2Id,SerialNumber,last_contract_number__c,Service_Start_Date_Min__c, Service_End_Date_Max__c                      
                         FROM Asset
                         WHERE                                                
                         last_contract_number__c = :C.Name
                        // last_contract_number__c =:GetContract.Name 
                         Limit 10];                      
    }  
  
}

Page looks as mentioned below

User-added image


My Requirement is I need to store the data tat is displayed on datatable. I must store only the records that are selected from using checkbox. 

I need to store to Tem_Contract__C is the object which has all the fields as displayed on screen. Please suggest me how to store data 

Thanks
Sudhir
Best Answer chosen by Sudhir_Meru
ShashForceShashForce
Hi Sudhir,

Like in the example code I provided, please try passing the account fields separately into the wrapper class instead of the whole account.

Something like:

public class wrapAccount {
        public Account acc {get; set;}
        public wrapAccName {get;set;}
        //etc.
        public Boolean selected {get; set;}

        public wrapAccount(Account a) {
            acc = a;
            wrapAccName = a.Name;
            //etc.
            selected = false;
        }
    }

Thanks,
Shashank

All Answers

ShashForceShashForce
Hi Sudhir,

You need to use something called a wrapper class for your need. Please see this example on how to implement a wrapper class: https://developer.salesforce.com/page/Wrapper_Class

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
Sonam_SFDCSonam_SFDC
Hi Sudhir,

I do not see a Wrapper class in your controller - to be able to work on the selected contracts from the datatable - you will have to encampus the Asset with the checkbox in a class and then process the records that get checked.

Read through the sample code in the link below which defines wrapper class:
https://developer.salesforce.com/page/Wrapper_Class
Sudhir_MeruSudhir_Meru
Thanks for your reply ill modify the code My Question is I am fetching data from Asset Object. Selected rows must be Inserted into Custom Object can implement? 
 
 Please give me a sample example 

Thanks
Sudhir
ShashForceShashForce
Hi Sudhir,

Once you fetch the data from Asset object and add the check box and use the wrapper class to represent the data along with the checkbox, you should be able to update the fields in your custom object.

Thanks,
Shashank
Sudhir_MeruSudhir_Meru

Hi Shashank,

   Please give me a sample code how it is save to custom object. I am new to wrapper code how to use. I will follow the same. 

Thanks

Sudhir

ShashForceShashForce
This is the wrapper class from the example I posted:

public class cContact {

        public Contact con {get; set;}
        public Boolean selected {get; set;}

        public cContact(Contact c) {

            con = c;
            selected = false;

        }
    }

For your Asset code, it may look something lilke this:

public class wrapAsset{

        public wrapId {get; set;}
        public wrapAccId {get; set;}
        public wrapProdId {get; set;}
        public wrapSerialNumber {get; set;},
        public wrapLastContractNumber {get; set;}
        public wrapServiceStartDateMin {get; set;}
        public wrapServiceEndDateMax {get; set;}
        public Boolean selected {get; set;}

        public wrapAsset(Asset a) {

            wrapId = a.Id;
            wrapAccId = a.AccountId;
            wrapProdId = a.Product2Id;
            wrapSerialNumber = a.SerialNumber;
            wrapLastContractNumber = a.last_contract_number__c;
            wrapServiceStartDateMin = a.Service_Start_Date_Min__c;
            wrapServiceEndDateMax = a.Service_End_Date_Max__c;
            selected = false;

        }
    }

We are basically generating a temporary object in our code to store the asset fields along with the checkbox.

Now, after your query on Asset, you can do a FOR loop through the query result to add records to the wrapper object list:

public List<wrapAsset> wrapList {get; set;}

for(Asset asst: Asset_yourObjList){
        wrapList.add( new wrapList(asst) );
    }

Now, you can create a list of your custom object and add these wrapper object records to the list to be inserted/updated, something like this:

list<Tem_Contract__c> temContList = new list<Tem_contract__c>;

for(wrapAsset wrAs:wrapList){
        Tem_Contract__c temCon = new Tem_Contract__c();
        temCon.AccountId = wrAs.wrapAccId;
        //and so on until...
        temCon.Service_End_Date_Max__c = wrAs.wrapServiceEndDateMax;

        temContList.add(temCon);
    }

Insert temConList;

This is just sample code, please customize it for your requirement, and relate to the example I previously posted, for better clarity. Hope this helps.

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
Sudhir_MeruSudhir_Meru

Wonderfull Shashank. I will implement same and update you in sometime. Thank you So much 

Thanks

Sudhir

Sudhir_MeruSudhir_Meru

Hi Shashank, 
  
  I was tring to insert account name into a temporary table based on selection made to test wrapper class but it is not inserting records. 

public class AccountSelectClassController{

    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Account> selectedAccounts{get;set;}

    public AccountSelectClassController(){
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account limit 10]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
    }

    public void processSelected() {
    selectedAccounts = new List<Account>();
    list<Temp_Asset__c> TempAssetList = new list<Temp_Asset__c>();
   
        for(wrapAccount wrapAccountObj : wrapAccountList) {
            if(wrapAccountObj.selected == true) {
                Temp_Asset__c TempAsset = new Temp_Asset__c();              
                selectedAccounts.add(wrapAccountObj.acc);
                TempAsset.Name = wrapAccountObj.acc.name; 
                TempAssetList.add(TempAsset);
            }
        }
        Insert TempAssetList;
    }

    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapAccount {
        public Account acc {get; set;}
        public Boolean selected {get; set;}

        public wrapAccount(Account a) {
            acc = a;
            selected = false;
        }
    }
}

Please tell me what is the mistake here  I will implement same for my controller on asset

Thanks

Sudhir

ShashForceShashForce
Hi Sudhir,

Like in the example code I provided, please try passing the account fields separately into the wrapper class instead of the whole account.

Something like:

public class wrapAccount {
        public Account acc {get; set;}
        public wrapAccName {get;set;}
        //etc.
        public Boolean selected {get; set;}

        public wrapAccount(Account a) {
            acc = a;
            wrapAccName = a.Name;
            //etc.
            selected = false;
        }
    }

Thanks,
Shashank
This was selected as the best answer
Sudhir_MeruSudhir_Meru
Thank You Very Much Shashank I was able to achive with you suggestion it was a great help for me. 
sachith ssachith s
Create custom object “Training Registration” and add some fields minimum five fields.
           And add one field named as “processed” with values ‘processed’ and ‘awaited’.
           Based On   this, query the records and list them in separate block.
           Records should be added to object through visualforce page And store them in object.
answer plz