<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Apex &#8220;Deep Clone&#8221; Controller</title>
	<atom:link href="http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=apex-deep-clone-controller</link>
	<description>Get your head out of your #@! and into the clouds!</description>
	<lastBuildDate>Wed, 08 Feb 2012 07:57:55 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
	<item>
		<title>By: Bharthi Ramsewak</title>
		<link>http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/comment-page-1/#comment-7498</link>
		<dc:creator>Bharthi Ramsewak</dc:creator>
		<pubDate>Mon, 12 Dec 2011 12:15:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=1710#comment-7498</guid>
		<description>Hi Jeff,

I have utilized your deep clone functionality to fit our organizations needs. 

Basically we have the following scenario :

We have a Quote Request, which has Vehicle Models related to it and you have accesories related to the models.

Below is the code we are using. The issue we are facing is if a quote has more than one Model it fails with the error :
Record ID: cannot specify Id in an insert call

The Code works if there is 1 model with 1 or more accessories attached to it. The issue comes in when there is 2 or more vehicle models attached to a quote. 

Is there any solution to the above issue we are facing?

===============================================
public class QuotationRequestCloneWithItemsController {

    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
     
    // add the instance for the variables being passed by id on the url
    private Quotation_Request__c qReq {get;set;}
    
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}

    // initialize the controller
    public QuotationRequestCloneWithItemsController(ApexPages.StandardController controller) {

        //initialize the stanrdard controller
        this.controller = controller;
        
        // load the current record
        qReq = (Quotation_Request__c)controller.getRecord();

    }

    // method called from the VF&#039;s action attribute to clone the Quote request
    public PageReference cloneWithItems() {

         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         Quotation_Request__c newqReq;

         try {

                              //copy the quote request - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
                             qReq = [select Account__c, 
                                     Arrears_Deal__c, 
                                     Call_Report__c, 
                                     Call_Report_Vehicle__c, 
                                     Canopies_Comments__c, 
                                     Canopies_Door__c, 
                                     Canopies_Material__c, 
                                     Canopies_Sliding_Windows__c, 
                                     Canopies_Windows__c, 
                                     Checked__c, 
                                     Contact_Person__c, 
                                     Customer__c, 
                                     Customer_Nominated_Accessory_Supplier__c, 
                                     Customer_Nominated_Supplier__c, 
                                     Date__c, 
                                     Deposit_Ex_VAT__c, 
                                     FAM__c, 
                                     Fuel_Cards__c, 
                                     Insurance__c, 
                                     Interest_Rate__c, 
                                     Lead__c, 
                                     License__c, 
                                     Medical_Aid_Kit__c, 
                                     Nominated_Residual__c, 
                                     Personalised_Number_Plates__c, 
                                     Product__c, 
                                     Prospects__c, 
                                     Quotation_Pricing_Method__c, 
                                     Name, 
                                     RV__c,
                                     ID, 
                                     Registered_Driver_Proxy__c, 
                                     Supplier_Name_Accessory__c, 
                                     Supplier_Name__c, 
                                     System_Parameters__c, 
                                     Tracking_Unit__c, 
                                     Tracking_Unit_Installation__c, 
                                     Tracking_Unit_Subscription__c, 
                                     Value_Add_Comment__c 
                                     from Quotation_Request__c where id = :qReq.id];
                             newqReq = qReq.clone(false);
                             insert newqReq;
                
                             //set the id of the new Quote Request to be inserted
                             newRecordId = newqReq.id;
                            
                                            
                           // copy over the models - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE -- 
                            List Models = new List();
                            List ModelAccessory = new List();
                            
                            for (Quote_Model__c qm : [Select Models__c,
                            name,
                            make_description__c,
                            X1_Contract_Months__c,
                            X1_KM__c,
                            X2_Contract_Months__c,
                            X2_KM__c,
                            X3_Contract_Months__c,
                            X3_KM__c,
                            X4_Contract_Months__c,
                            X4_KM__c,No_of_Units__c,
                            m_m_number__c,
                            Required_Delivery_Date__c,
                            Delivery_Province__c,
                            Availability__c,
                            Required_Delivery_Area__c,
                            Comments__c,
                            Discontinued_Date__c,
                            Barred_Date__c
                            from Quote_Model__c  
                            where Quotation_Request__c = :qReq.id]) 
                                                                                                  
                                  {                                    
                                    Quote_Model__c newQM = qm.clone(false);
                                    newQM.Quotation_Request__c = newqReq.id;
                                    Models.add(newQM);
                                    insert Models;                                    
                
                                    //get accessories per model
                                    for (Accessory__c qmAcc : [SELECT Accessory_Number__c,
                                                               Canopies_Comments__c,Canopies_Door__c,
                                                               Quotation_Request__c,Quote_Model__c,
                                                               Accessory_Type__c,
                                                               Accessory_Details__c,
                                                               Accessory_Comments__c,
                                                               item_selection__c,
                                                               Other_Accessories__c FROM Accessory__c
                                                               where Quote_Model__c = :qm.id]) 
                                                 {                       
                                                   Accessory__c clonedAccessory = qmAcc.clone(false);
                                                   clonedAccessory.Quote_Model__c = newQM.id;
                                                   ModelAccessory.add(clonedAccessory);
                                                 }
                                      
                                 }
                              insert ModelAccessory;
                
                



         } catch (Exception e){
             // roll everything back in case of error
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
         }

        return new PageReference(&#039;/&#039;+newqReq.id+&#039;/e?retURL=%2F&#039;+newqReq.id);
    }

}</description>
		<content:encoded><![CDATA[<p>Hi Jeff,</p>
<p>I have utilized your deep clone functionality to fit our organizations needs. </p>
<p>Basically we have the following scenario :</p>
<p>We have a Quote Request, which has Vehicle Models related to it and you have accesories related to the models.</p>
<p>Below is the code we are using. The issue we are facing is if a quote has more than one Model it fails with the error :<br />
Record ID: cannot specify Id in an insert call</p>
<p>The Code works if there is 1 model with 1 or more accessories attached to it. The issue comes in when there is 2 or more vehicle models attached to a quote. </p>
<p>Is there any solution to the above issue we are facing?</p>
<p>===============================================<br />
public class QuotationRequestCloneWithItemsController {</p>
<p>    //added an instance varaible for the standard controller<br />
    private ApexPages.StandardController controller {get; set;}</p>
<p>    // add the instance for the variables being passed by id on the url<br />
    private Quotation_Request__c qReq {get;set;}</p>
<p>    // set the id of the record that is created &#8212; ONLY USED BY THE TEST CLASS<br />
    public ID newRecordId {get;set;}</p>
<p>    // initialize the controller<br />
    public QuotationRequestCloneWithItemsController(ApexPages.StandardController controller) {</p>
<p>        //initialize the stanrdard controller<br />
        this.controller = controller;</p>
<p>        // load the current record<br />
        qReq = (Quotation_Request__c)controller.getRecord();</p>
<p>    }</p>
<p>    // method called from the VF&#8217;s action attribute to clone the Quote request<br />
    public PageReference cloneWithItems() {</p>
<p>         // setup the save point for rollback<br />
         Savepoint sp = Database.setSavepoint();<br />
         Quotation_Request__c newqReq;</p>
<p>         try {</p>
<p>                              //copy the quote request &#8211; ONLY INCLUDE THE FIELDS YOU WANT TO CLONE<br />
                             qReq = [select Account__c,<br />
                                     Arrears_Deal__c,<br />
                                     Call_Report__c,<br />
                                     Call_Report_Vehicle__c,<br />
                                     Canopies_Comments__c,<br />
                                     Canopies_Door__c,<br />
                                     Canopies_Material__c,<br />
                                     Canopies_Sliding_Windows__c,<br />
                                     Canopies_Windows__c,<br />
                                     Checked__c,<br />
                                     Contact_Person__c,<br />
                                     Customer__c,<br />
                                     Customer_Nominated_Accessory_Supplier__c,<br />
                                     Customer_Nominated_Supplier__c,<br />
                                     Date__c,<br />
                                     Deposit_Ex_VAT__c,<br />
                                     FAM__c,<br />
                                     Fuel_Cards__c,<br />
                                     Insurance__c,<br />
                                     Interest_Rate__c,<br />
                                     Lead__c,<br />
                                     License__c,<br />
                                     Medical_Aid_Kit__c,<br />
                                     Nominated_Residual__c,<br />
                                     Personalised_Number_Plates__c,<br />
                                     Product__c,<br />
                                     Prospects__c,<br />
                                     Quotation_Pricing_Method__c,<br />
                                     Name,<br />
                                     RV__c,<br />
                                     ID,<br />
                                     Registered_Driver_Proxy__c,<br />
                                     Supplier_Name_Accessory__c,<br />
                                     Supplier_Name__c,<br />
                                     System_Parameters__c,<br />
                                     Tracking_Unit__c,<br />
                                     Tracking_Unit_Installation__c,<br />
                                     Tracking_Unit_Subscription__c,<br />
                                     Value_Add_Comment__c<br />
                                     from Quotation_Request__c where id = :qReq.id];<br />
                             newqReq = qReq.clone(false);<br />
                             insert newqReq;</p>
<p>                             //set the id of the new Quote Request to be inserted<br />
                             newRecordId = newqReq.id;</p>
<p>                           // copy over the models &#8211; ONLY INCLUDE THE FIELDS YOU WANT TO CLONE &#8212;<br />
                            List Models = new List();<br />
                            List ModelAccessory = new List();</p>
<p>                            for (Quote_Model__c qm : [Select Models__c,<br />
                            name,<br />
                            make_description__c,<br />
                            X1_Contract_Months__c,<br />
                            X1_KM__c,<br />
                            X2_Contract_Months__c,<br />
                            X2_KM__c,<br />
                            X3_Contract_Months__c,<br />
                            X3_KM__c,<br />
                            X4_Contract_Months__c,<br />
                            X4_KM__c,No_of_Units__c,<br />
                            m_m_number__c,<br />
                            Required_Delivery_Date__c,<br />
                            Delivery_Province__c,<br />
                            Availability__c,<br />
                            Required_Delivery_Area__c,<br />
                            Comments__c,<br />
                            Discontinued_Date__c,<br />
                            Barred_Date__c<br />
                            from Quote_Model__c<br />
                            where Quotation_Request__c = :qReq.id]) </p>
<p>                                  {<br />
                                    Quote_Model__c newQM = qm.clone(false);<br />
                                    newQM.Quotation_Request__c = newqReq.id;<br />
                                    Models.add(newQM);<br />
                                    insert Models;                                    </p>
<p>                                    //get accessories per model<br />
                                    for (Accessory__c qmAcc : [SELECT Accessory_Number__c,<br />
                                                               Canopies_Comments__c,Canopies_Door__c,<br />
                                                               Quotation_Request__c,Quote_Model__c,<br />
                                                               Accessory_Type__c,<br />
                                                               Accessory_Details__c,<br />
                                                               Accessory_Comments__c,<br />
                                                               item_selection__c,<br />
                                                               Other_Accessories__c FROM Accessory__c<br />
                                                               where Quote_Model__c = :qm.id])<br />
                                                 {<br />
                                                   Accessory__c clonedAccessory = qmAcc.clone(false);<br />
                                                   clonedAccessory.Quote_Model__c = newQM.id;<br />
                                                   ModelAccessory.add(clonedAccessory);<br />
                                                 }</p>
<p>                                 }<br />
                              insert ModelAccessory;</p>
<p>         } catch (Exception e){<br />
             // roll everything back in case of error<br />
            Database.rollback(sp);<br />
            ApexPages.addMessages(e);<br />
            return null;<br />
         }</p>
<p>        return new PageReference(&#8216;/&#8217;+newqReq.id+&#8217;/e?retURL=%2F&#8217;+newqReq.id);<br />
    }</p>
<p>}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Douglas</title>
		<link>http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/comment-page-1/#comment-4812</link>
		<dc:creator>Jeff Douglas</dc:creator>
		<pubDate>Sat, 24 Sep 2011 12:06:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=1710#comment-4812</guid>
		<description>@Subha, this is intended functionality. If you want to have them preview the new record before creating it then you will have to write a controller and Visulforce page that preloads all of the info, allows them to make edits and then saves the record.</description>
		<content:encoded><![CDATA[<p>@Subha, this is intended functionality. If you want to have them preview the new record before creating it then you will have to write a controller and Visulforce page that preloads all of the info, allows them to make edits and then saves the record.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Subha</title>
		<link>http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/comment-page-1/#comment-4689</link>
		<dc:creator>Subha</dc:creator>
		<pubDate>Tue, 13 Sep 2011 17:47:34 +0000</pubDate>
		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=1710#comment-4689</guid>
		<description>Hi Jeff, thanks for the code.The problem I am facing is after clicking the clone button a new record is created even if i click cancel button also this is because of the statement insert newPO but I am not getting how to solve this problem.Can you please help me...</description>
		<content:encoded><![CDATA[<p>Hi Jeff, thanks for the code.The problem I am facing is after clicking the clone button a new record is created even if i click cancel button also this is because of the statement insert newPO but I am not getting how to solve this problem.Can you please help me&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony Coleman</title>
		<link>http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/comment-page-1/#comment-4666</link>
		<dc:creator>Tony Coleman</dc:creator>
		<pubDate>Thu, 08 Sep 2011 20:19:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=1710#comment-4666</guid>
		<description>Check this out for easy deep cloning:
http://blogs.developerforce.com/developer-relations/2009/02/cloning-objects-in-salesforcecom.html</description>
		<content:encoded><![CDATA[<p>Check this out for easy deep cloning:<br />
<a href="http://blogs.developerforce.com/developer-relations/2009/02/cloning-objects-in-salesforcecom.html" rel="nofollow">http://blogs.developerforce.com/developer-relations/2009/02/cloning-objects-in-salesforcecom.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin</title>
		<link>http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/comment-page-1/#comment-3850</link>
		<dc:creator>Kevin</dc:creator>
		<pubDate>Thu, 19 May 2011 16:04:01 +0000</pubDate>
		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=1710#comment-3850</guid>
		<description>Jeff,

Thank you for your efforts and the education. I love reading and learning from these posts. I&#039;m needing to clone opportunities and a custom related object (not master-detail but rather a lookup relationship). It seems that this method would work well for that. Anything I&#039;m missing?</description>
		<content:encoded><![CDATA[<p>Jeff,</p>
<p>Thank you for your efforts and the education. I love reading and learning from these posts. I&#8217;m needing to clone opportunities and a custom related object (not master-detail but rather a lookup relationship). It seems that this method would work well for that. Anything I&#8217;m missing?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Douglas</title>
		<link>http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/comment-page-1/#comment-3196</link>
		<dc:creator>Jeff Douglas</dc:creator>
		<pubDate>Wed, 13 Apr 2011 00:18:15 +0000</pubDate>
		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=1710#comment-3196</guid>
		<description>Sure! It shouldn&#039;t be a problem. Just add the additional query once you create the opportunity.</description>
		<content:encoded><![CDATA[<p>Sure! It shouldn&#8217;t be a problem. Just add the additional query once you create the opportunity.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Venu</title>
		<link>http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/comment-page-1/#comment-3111</link>
		<dc:creator>Venu</dc:creator>
		<pubDate>Tue, 29 Mar 2011 19:25:07 +0000</pubDate>
		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=1710#comment-3111</guid>
		<description>Hi Jeff,

Can we extend this code to do the deeper clone like 

Account --&gt; opportunity --&gt; opportunitylineitems</description>
		<content:encoded><![CDATA[<p>Hi Jeff,</p>
<p>Can we extend this code to do the deeper clone like </p>
<p>Account &#8211;&gt; opportunity &#8211;&gt; opportunitylineitems</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pat M</title>
		<link>http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/comment-page-1/#comment-3104</link>
		<dc:creator>Pat M</dc:creator>
		<pubDate>Mon, 28 Mar 2011 18:50:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=1710#comment-3104</guid>
		<description>Hi Jeff, Thanks for your posts, I always find them very useful.  I have shamelessly copied your code to custom clone opportunities however, the savepoint is not working for me.  If my user clicks Cancel, they are returned to the original record but there is still a clone of the opportunity.  Any ideas what I could be missing?  Thanks!</description>
		<content:encoded><![CDATA[<p>Hi Jeff, Thanks for your posts, I always find them very useful.  I have shamelessly copied your code to custom clone opportunities however, the savepoint is not working for me.  If my user clicks Cancel, they are returned to the original record but there is still a clone of the opportunity.  Any ideas what I could be missing?  Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Venu</title>
		<link>http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/comment-page-1/#comment-2904</link>
		<dc:creator>Venu</dc:creator>
		<pubDate>Thu, 17 Mar 2011 22:47:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=1710#comment-2904</guid>
		<description>Great Post Jeff, This is very helpful for my application.

I have one more question, can we do deeper clone up to three levels?

If so do we hit the governor limits for SOQL Queries?</description>
		<content:encoded><![CDATA[<p>Great Post Jeff, This is very helpful for my application.</p>
<p>I have one more question, can we do deeper clone up to three levels?</p>
<p>If so do we hit the governor limits for SOQL Queries?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JP</title>
		<link>http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/comment-page-1/#comment-2887</link>
		<dc:creator>JP</dc:creator>
		<pubDate>Wed, 16 Mar 2011 17:59:46 +0000</pubDate>
		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=1710#comment-2887</guid>
		<description>Can this be re purposed to clone the related records of another related object? 

Specifically, I have a template of related records related to a parent (Object 1). I want to relate that parent record (Object 1 and Object 2) and copy the related records into another related list of records that is a child of Object 2. 

thank you for any input you can provide</description>
		<content:encoded><![CDATA[<p>Can this be re purposed to clone the related records of another related object? </p>
<p>Specifically, I have a template of related records related to a parent (Object 1). I want to relate that parent record (Object 1 and Object 2) and copy the related records into another related list of records that is a child of Object 2. </p>
<p>thank you for any input you can provide</p>
]]></content:encoded>
	</item>
</channel>
</rss>

