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
Mohit Kashyap 14Mohit Kashyap 14 

Suppose in Opportunity we have related list of custom objects and if I clone the opportunity the related list should also be cloned. I had figured out that we can do this with VF. But dont know how to do it. Can someone plzz help?

Suppose in Opportunity we have related list of custom objects and if I clone the opportunity the related list should also be cloned. I had figured out that we can do this with VF. But dont know how to do it. Can someone plzz help?
Best Answer chosen by Mohit Kashyap 14
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mohit,
By using VF page we can close as below.

vf page
<apex:page controller="Sample" sidebar="false">

<apex:form >
    <apex:pageblock >
        <apex:pageblockSection >
            <apex:pageblockSectionItem >Enter the Opportunity Id:</apex:pageblockSectionItem>
            <apex:pageblockSectionItem ><apex:inputtext value="{!idOfRec}" /></apex:pageblockSectionItem>           
        </apex:pageblockSection>
        <apex:pageblockButtons >
            <apex:commandButton value="Clone" action="{!cloneRec}"/>
        </apex:pageblockButtons>
    </apex:pageblock>  
</apex:form>

</apex:page>

Controller:
 
public class Sample
{  
    public String idOfRec {get;set;}
  
    public Sample()
    {
    }
  
    public void cloneRec()
    {
        List<Quote__c> cons = new List<Quote__c>();
        Opportunity acc = [SELECT ID, Name,StageName, CloseDate  FROM Opportunity WHERE Id = : idOfRec];
        Opportunity accCopy = acc.clone(false,true);
        insert accCopy;
        List<Quote__c > quote = [SELECT Id, Name,status__c  FROM Quote__c WHERE Opportunity__c = : acc.Id];
        for(Quote__c c : quote)
        {
            Quote__c conCopy = c.clone(false,true);
            conCopy.Opportunity__c = accCopy.Id;
            cons.add(conCopy);
        }
        insert cons;
    }
}

We an also achieve it clicking on a link on the record page by using the below link.

https://developer.salesforce.com/forums/?id=906F0000000BTJ8IAO

If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mohit,
By using VF page we can close as below.

vf page
<apex:page controller="Sample" sidebar="false">

<apex:form >
    <apex:pageblock >
        <apex:pageblockSection >
            <apex:pageblockSectionItem >Enter the Opportunity Id:</apex:pageblockSectionItem>
            <apex:pageblockSectionItem ><apex:inputtext value="{!idOfRec}" /></apex:pageblockSectionItem>           
        </apex:pageblockSection>
        <apex:pageblockButtons >
            <apex:commandButton value="Clone" action="{!cloneRec}"/>
        </apex:pageblockButtons>
    </apex:pageblock>  
</apex:form>

</apex:page>

Controller:
 
public class Sample
{  
    public String idOfRec {get;set;}
  
    public Sample()
    {
    }
  
    public void cloneRec()
    {
        List<Quote__c> cons = new List<Quote__c>();
        Opportunity acc = [SELECT ID, Name,StageName, CloseDate  FROM Opportunity WHERE Id = : idOfRec];
        Opportunity accCopy = acc.clone(false,true);
        insert accCopy;
        List<Quote__c > quote = [SELECT Id, Name,status__c  FROM Quote__c WHERE Opportunity__c = : acc.Id];
        for(Quote__c c : quote)
        {
            Quote__c conCopy = c.clone(false,true);
            conCopy.Opportunity__c = accCopy.Id;
            cons.add(conCopy);
        }
        insert cons;
    }
}

We an also achieve it clicking on a link on the record page by using the below link.

https://developer.salesforce.com/forums/?id=906F0000000BTJ8IAO

If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer
Mohit Kashyap 14Mohit Kashyap 14

 Sample Compile Error: SELECT Id, Name FROM car__c WHERE Opportunity__c = : acc.Id ^ ERROR at Row:1:Column:35 No such column 'Opportunity__c' on entity 'car__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 15 column 31

I am getting this error. Instead of Quote__c I am using a custom object car__c. What all fields should I include in the custom object? 
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mohit,

Do we have opportinuty field on Car__c object. If yes, Just replace the API name of it.

Thanks,

 
Mohit Kashyap 14Mohit Kashyap 14
No we dont have Opportunity field on Car__c object ..Do I need to create a lookup Opportunity field ?
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mohit,  

If we dont have relation between opportunity and car how is cars related list available in opportunity. There may be some change in the name . If not please create a relation between opportunity and car then only cars related list will be available in opportunity record.

Thanks,
 
Mohit Kashyap 14Mohit Kashyap 14
I made a lookup relationship so the Opportunity is the parent and the child is the car__c. Now if enter the Opp id , it is working fine that opprtunity is getting cloned but car__ remains as it is. It should also be cloned.
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mohit,

Can you please post  your code here once.

Thanks,
 
Mohit Kashyap 14Mohit Kashyap 14
User Interface:
<apex:page controller="Sample" sidebar="false">
<apex:form >
    <apex:pageblock >
        <apex:pageblockSection >
            <apex:pageblockSectionItem >Enter the Opportunity Id:</apex:pageblockSectionItem>
            <apex:pageblockSectionItem ><apex:inputtext value="{!idOfRec}" /></apex:pageblockSectionItem>           
        </apex:pageblockSection>
        <apex:pageblockButtons >
            <apex:commandButton value="Clone" action="{!cloneRec}"/>
        </apex:pageblockButtons>
    </apex:pageblock>  
</apex:form>

</apex:page>


Controller class
public class Sample
{  
    public String idOfRec {get;set;}
  
    public Sample()
    {
    }
  
    public void cloneRec()
    {
        List<Car__c> cons = new List<Car__c>();
        Opportunity acc = [SELECT ID, Name,StageName, CloseDate  FROM Opportunity WHERE Id = : idOfRec];
        Opportunity accCopy = acc.clone(false,true);
        insert accCopy;
        List<car__c > quote = [SELECT Id, Name FROM car__c WHERE Opportunity__c = : acc.Id];
        for(car__c c : quote)
        {
            car__c conCopy = c.clone(false,true);
            conCopy.Opportunity__c = accCopy.Id;
            cons.add(conCopy);
        }
        insert cons;
    }
}
Mohit Kashyap 14Mohit Kashyap 14
*User Interface:* Enter the Opportunity Id: *Controller class* public class Sample { public String idOfRec {get;set;} public Sample() { } public void cloneRec() { List cons = new List(); Opportunity acc = [SELECT ID, Name,StageName, CloseDate FROM Opportunity WHERE Id = : idOfRec]; Opportunity accCopy = acc.clone(false,true); insert accCopy; List quote = [SELECT Id, Name FROM car__c WHERE Opportunity__c = : acc.Id]; for(car__c c : quote) { car__c conCopy = c.clone(false,true); conCopy.Opportunity__c = accCopy.Id; cons.add(conCopy); } insert cons; } }
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mohit,

Do you have cars related list and any cars associated to the opportunity. I used the same code and able to clone the cars records as well.

User-added image
Thanks
Mohit Kashyap 14Mohit Kashyap 14
Yes I am now able to do it . Thank you so much Sai.
 
Mohit Kashyap 14Mohit Kashyap 14
Hey, Now I want to do the same thing with Javascript button and instead for One custom object, I want it for other objects as well .
Example: when I clone a Opportunity, and lets say there are five custom object (realted List) that should also be cloned.
I am attaching the sample code which I had written:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
sforce.apex.execute("Sample","cloneRec",{{!Opportunity.Id}:{!Opportunity.Id}}"});



Controller:
public class Sample
{  
    public String idOfRec {get;set;}
  
    public Sample()
    {
    }
  
    public void cloneRec()
    {
        List<Car__c> cons = new List<Car__c>();
        Opportunity acc = [SELECT ID, Name,StageName, CloseDate  FROM Opportunity WHERE Id = : idOfRec];
        Opportunity accCopy = acc.clone(false,true);
        insert accCopy;
        List<car__c > quote = [SELECT Id, Name FROM car__c WHERE Opportunity__c = : acc.Id];
        for(car__c c : quote)
        {
            car__c conCopy = c.clone(false,true);
            conCopy.Opportunity__c = accCopy.Id;
            cons.add(conCopy);
        }
        insert cons;
    }
}

Can you please help me with this