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
pathi bahubhalipathi bahubhali 

problem with cloning account with associated opportunites

My requirement ,
I have one  clone button, when i click clone button from account detail page it has to copy the account and it's associated contacts ,opportunities.
Here i have written some peice of code but it's not working please help me regarding this.
From this code i am able to clone account with associated contacts but opprtunites.
VF page is :
<apex:page StandardController="Account" extensions="Sample" sidebar="false" showHeader="false">
url={!$CurrentPage.parameters.Id}

<apex:form >

    <apex:pageblock id="pb">
        
              <apex:outputLabel >Enter The Account Id:</apex:outputLabel>&nbsp;&nbsp;   
              <apex:inputText value="{!idOfRec}" />            
        
        <apex:pageblockButtons location="top"><br/><br/><br/><br/><br/>
            <apex:commandButton value="Are You Sure" action="{!cloneRec}" />
            <apex:commandButton value="Cancel" action="{!cancel}"/>
        </apex:pageblockButtons>
    </apex:pageblock>   
</apex:form>

</apex:page>

Apex Class:
public class Sample1
{ 
   public string Id{set;get;}
     public Sample1(ApexPages.StandardController controller) {

  
    }
    
    public String idOfRec{set;get;} 
             
   
    public pagereference cloneRec()
    {
       list<contact> cons=new list<contact>();
        list<opportunity> opp=new list<opportunity>();
   
      Schema.Account acc = [SELECT ID, Name FROM Account WHERE Id = : idOfRec];
        Schema.Account accCopy = acc.clone(false,true);
        insert accCopy;
         list<contact> con = [SELECT Id, LastName, AccountId FROM Contact WHERE AccountId = : acc.Id];
         list<opportunity> opp1=[select Id, Name, AccountId FROM opportunity WHERE AccountId = : acc.Id];
       
        for(Contact c : con)
        {
            Contact conCopy = c.clone(false,true);
            conCopy.AccountId = accCopy.Id;
            cons.add(conCopy);
        }
        insert cons;
        for(opportunity op : opp1)
        {
            opportunity oppCopy = op.clone(false,true);
            oppCopy.AccountId = accCopy.Id;
            opp.add(oppCopy);
        }
        insert opp;
        pagereference pg=new pagereference('/'+accCopy.id);
        pg.setredirect(true);
        return pg;
    
       }
}
Thanks in Advance......!
kattappa
 
James LoghryJames Loghry
Kattappa,

How is it not working?  Does the code compile? Does the code throw an exception?  Is it not inserting contacts as you'd expect?  Is it not inserting opportunities as you'd expect?  Simply saying "it does not work" is being very vague and we need more details if we're truly going to help solve your issue.

Off the bat however, I do see that you're inserting "opp" at line 36 in your example.  This will only insert a single record.  Believe you want it to insert "opps" instead.
pathi bahubhalipathi bahubhali
No, opp is inserting list of records