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
jack2000jack2000 

Visualforce page with Apex custom controller

Hi there. I m newbie in developement via this platform, so be patient.

 

I trying to create simple Visualforce page that contains a table with my merchandises (like a Warehouse testing app in workbook) and control buttons on this page. Control buttons are "Edit", "Save", "New", "Find", "Delete" and others like in standart controllers.

How to create logic to this buttons? I was created "Find' button and added logic. Thiis was simple.

But how to create logic in apex class to save edited row in table? In each colomn i added   <apex:inlineEditSupport showOnEdit="saveButton, cancelButton" hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>, so i can edit some rows in some columns that i need.

But how to insert changes in table? Where i need to find variables, that contains number of row edited and name of the column?

 

So, please help me - tell, or show the sources, how to realise that logic.

Same problems with  "Add new object to table"   and "Delete object from table" logic .

My Visualforce page source code:

<apex:page standardStylesheets="false" showHeader="false" sidebar="false"
           controller="VRP_controller" >
    <apex:stylesheet value="{!URLFOR($Resource.styles, 'styles.css')}"/>
    <h1>Headphones Shop</h1>
    <apex:form >
   <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
        <apex:column headerValue="Product">
            <apex:outputfield value="{!pitem.merchandise.name}">
            <apex:inlineEditSupport showOnEdit="saveButton, cancelButton" hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
          </apex:outputField>
        </apex:column>         
        <apex:column headerValue="Product type">
            <apex:outputField value="{!pitem.merchandise.Product_Type__c}">
       <apex:inlineEditSupport showOnEdit="saveButton, cancelButton" hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
          </apex:outputField>
        </apex:column>
        <apex:column headerValue="Count in Stock">
            <apex:outputField value="{!pitem.merchandise.Product_Count__c}">
             <apex:inlineEditSupport showOnEdit="saveButton, cancelButton" hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
          </apex:outputField>
           </apex:column>
        <apex:column headerValue="Available">
            <apex:outputText value="{!pitem.merchandise.In_Stock__c}"/>
                     </apex:column>
        <apex:column headerValue="Release Date">
            <apex:outputText value="{0,date,yyyy.MM.dd}">
 <apex:param value="{!pitem.merchandise.Release_Date__c}" />
     
   </apex:outputText>
        </apex:column>
        <apex:column headerValue="Date Added">
            <apex:outputField value="{!pitem.merchandise.Date_Added__c}">
       <apex:inlineEditSupport showOnEdit="saveButton, cancelButton" hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
          </apex:outputField>
         </apex:column>
        <apex:column headerValue="Description">
            <apex:outputField value="{!pitem.merchandise.Description__c}"/>
       
        </apex:column>
       
        
</apex:dataTable>
<br />
<apex:PageBlock >

                <apex:commandButton value="Edit" action="{!save}" id="editButton" />
                <apex:commandButton value="Save" action="{!save}" id="saveButton" />
                <apex:commandButton value="Cancel" action="{!cancel}" id="cancelButton" /> 

</apex:PageBlock>


<apex:inputText value="{!textarea}" />
   
<apex:commandButton action="{!find}" value="Find" />
<apex:commandButton action="{!back}" value="Back" />


    </apex:form>
</apex:page>

 My Apex cusom controller code:

public class VRP_controller {

    public PageReference cancel() {
        return null;
    }

  
    
    public String message { get; set; }
    public String eval { get {return eval;} set{eval ='%'+value+'%';} }
   public String textarea { get {return textarea;} set{textarea = value;} }
    public PageReference back() {
      products=null;
       getProducts();
       return null;
    }
   
     public PageReference save() {
   for (DisplayMerchandise item :
            products) {

             insert item.merchandise;
             }
       return null;
    }
     public PageReference find() {
         eval=textarea;
       findProducts();
                
     return null;
    }  
        
    DisplayMerchandise[] products;
    DisplayMerchandise[] findedProducts;
    DisplayMerchandise[] backupProducts;
    public static boolean lock=false;
     
    
      
    public class DisplayMerchandise {
           
         public VRP_testing_app__c merchandise { get; set; }
        public DisplayMerchandise(VRP_testing_app__c item) {
            this.merchandise = item;
        }
   
    }

    public DisplayMerchandise[] getProducts() {
        if(products==null){
          products = new DisplayMerchandise[]{};
           for (VRP_testing_app__c item :
            [SELECT Name, Product_Type__c, Product_Count__c, In_Stock__c, Release_Date__c,Date_Added__c,Description__c
             FROM VRP_testing_app__c
             WHERE  In_Stock__c=true]) {

               products.add(new DisplayMerchandise(item));
             }
         }
     return products;      
    }
    
    public DisplayMerchandise[] findProducts(){
           
           findedProducts = new DisplayMerchandise[]{};
             for (VRP_testing_app__c item :
            [SELECT Name, Product_Type__c, Product_Count__c, In_Stock__c, Release_Date__c,Date_Added__c,Description__c
             FROM VRP_testing_app__c
             WHERE  (Name LIKE :eval)]){
              findedProducts.add(new DisplayMerchandise(item));
           }
         products=findedProducts;
     return findedProducts;      
    }
        
    }

 I know that is the bad style programming code, but i'm only studying.

Sorry for bad english - is not my native language.

Best Answer chosen by Admin (Salesforce Developers) 
liron169liron169

1.If you doing changes to existing record you should
do update, not insert.

2.also, highly recommaned, not to do it inside loop.
instead:
for (DisplayMerchandise item :
            products) {

             insert item.merchandise;
             }

write:
List<Merchandise> merch_lst=new List<Merchandise>();
for (DisplayMerchandise item : products) {
     merch_lst.add(item.merchandise);
}
insert merch_lst;

All Answers

liron169liron169

1.If you doing changes to existing record you should
do update, not insert.

2.also, highly recommaned, not to do it inside loop.
instead:
for (DisplayMerchandise item :
            products) {

             insert item.merchandise;
             }

write:
List<Merchandise> merch_lst=new List<Merchandise>();
for (DisplayMerchandise item : products) {
     merch_lst.add(item.merchandise);
}
insert merch_lst;

This was selected as the best answer
jack2000jack2000
Thank you very much. Another question: how to sort merchandises by a column values? Only by creating SOQL queries that contains "ORDER BY", or maybe having another solution?
liron169liron169

I didn't try yet to do sorting in page.

 

but maybe this link will help:

http://salesforcesource.blogspot.co.il/2008/11/adding-sorting-capability-to.html

 

or you can try google it, I'm sure that someone already found solution for that issue.