2

I'm very noob with apex and VF. My problem is:

I've a pageBlockTable that shows a list of record from a Custom Object. Now I've a column of the table on which there's a button ("Clone") that should perform a clone action so that If I push the button the system should clone the record of the row for which I push the button and should take me to edit page for the new cloned record .

I'm going crazy with this!

Class:

public virtual class QuotationController {

    public Quotation__c myq {get; set;}

    public QuotationController () {

    }

    public List<Quotation__c> getListaQuotation() {

      return [select Id, Name, Q_Product_Consultant_costs__c, Q_Product_Program_Director_costs__c, Q_Products_total_costs__c,
       Q_Product_Project_Manager_costs__C, Q_Product_Senior_Graphic_Designer_costs__c, (SELECT Name, Product_Type__c
        from Q_Products__r)
                         from Quotation__c 
                         WHERE IsTemplate__c = true
                         ]; 
    }
}

Page:

<apex:page Controller="QuotationController" tabstyle="Opportunity" >

<apex:form >
<apex:pageBlock title="Quotation Template"> 

<apex:pageBlockSection title="Available Starting Template">
  <apex:pageBlockTable value="{!ListaQuotation}" var="f">

   <apex:column value="{!f.Name}">   
     </apex:column>  

   <apex:column value="{!f.Q_Product_Consultant_costs__c}">
   </apex:column> 
    <apex:column value="{!f.Q_Product_Program_Director_costs__c}">
   </apex:column> 
    <apex:column value="{!f.Q_Product_Project_Manager_costs__c}">
   </apex:column> 
    <apex:column value="{!f.Q_Product_Senior_Graphic_Designer_costs__c}">
   </apex:column>   

   <apex:column value="{!f.Q_Products_total_costs__c}">
   </apex:column>     
    <apex:column >
            <apex:facet name="header">Related Products</apex:facet>

                <ul>
                    <apex:repeat value="{!f.Q_Products__r}" var="qp">
                        <li><apex:outputField value="{!qp.Product_Type__c}" /></li>                        
                    </apex:repeat>
                </ul>
            </apex:column>    


    <apex:column >

    <apex:commandButton value="Select" id="a"/> 

    </apex:column>     

</apex:pageBlockTable>
</apex:pageBlockSection>

</apex:pageBlock>


</apex:form>

</apex:page>
2
  • Could we see your code ? – Samuel De Rycke Jun 18 '14 at 9:12
  • I've posted the code. I would like to use the button on the last column for cloning the record of the row on which the button is – user9959 Jun 18 '14 at 9:42
1

Assuming you have an object id you want to clone. Then you can try to use some URL tricks. Create a normal html link (and put it into the pageblock column) with following href parameter:

<a href="/{!originalObjectId}/e?clone=1" target="_blank">Clone</a>

After clicking on the link a new page will open with preselected fields from the original record. The magic here is that we use /e?clone=1 URL parameter. /e means an "edit mode" and clone=1 means clone the original record.

So depending on your code it should look like this:

<apex:column >
    <apex:commandButton value="Clone" 
                        id="a"
                        onclick="window.open('/{!f.Id}/e?clone=1'); return false;"/> 
</apex:column>  
2
  • thanks! now, I'd like that the cloned record would be saved automatically, and i d like to be redirected to the detail page of the new cloned record (already saved) . Is it possible? – user9959 Jun 18 '14 at 9:55
  • 1
    @toninotrip This is possible using Apex code. Read about clone method of the sObject class. – Sergej Utko Jun 18 '14 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy