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 

when I clone an Opportunity all the related list should also be cloned

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

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

It is giving me the error when I click on " Custom Cloning Button "
A problem with the onclick javascript for this button or link was encountered 
Invalid or unexpected token
Suraj Tripathi 47Suraj Tripathi 47
Hi Mohit,
you can take reference from this code, it will helps you :
Code:
public class insertOpportunityLineItem {
    public static void insertOppoLineItem(){
        try{
            //create new product
            Product2 Obj_product=new Product2();
            Obj_product.Name='How to solution website';
            Obj_product.IsActive=true;
            Obj_product.Family='None';
            insert Obj_product; 	//productObj;
            system.debug(Obj_product);
            
            //get standard pricebook
           	Pricebook2 standardPricebookObj=[select id,name from Pricebook2 where isStandard=true limit 1];
            system.debug(standardPricebookObj);
           
            
            
            //PricebookEntry 
            PricebookEntry obj_pricebookEntry = new PricebookEntry();
            obj_pricebookEntry.Pricebook2Id = standardPricebookObj.Id;
            obj_pricebookEntry.Product2Id = Obj_product.Id;
            obj_pricebookEntry.IsActive = true;
            obj_pricebookEntry.UnitPrice = 1;
            insert obj_pricebookEntry;
            system.debug(obj_pricebookEntry);
            
            //create opportunity 
            Opportunity obj_opportunity = new Opportunity();
            obj_opportunity.Name = 'Oppertunity 2';
            obj_opportunity.CloseDate = System.today();
            obj_opportunity.StageName = 'Closed Won';
            insert obj_opportunity;
            system.debug(obj_opportunity);
            
            
            //opportunity line item 
            OpportunityLineItem obj_opportunityLineItem = new OpportunityLineItem();
            obj_opportunityLineItem.PricebookEntryId = obj_pricebookEntry.Id;
            obj_opportunityLineItem.OpportunityId = obj_opportunity.Id;
            obj_opportunityLineItem.Quantity = 10;
            obj_opportunityLineItem.UnitPrice = 1;
            insert obj_opportunityLineItem;
            system.debug(obj_opportunityLineItem);



            
            //Cloning Oppertunity
            Opportunity clone_Oppertunity = new Opportunity();
            clone_Oppertunity.Name = obj_opportunity.Name+'Clone';
            clone_Oppertunity.CloseDate = obj_opportunity.CloseDate.addDays(30);
            clone_Oppertunity.StageName = 'Closed Won';
            insert clone_Oppertunity;
            system.debug(clone_Oppertunity);
            
            //Creating NewOpportunityLineItem
            OpportunityLineItem cloneOpportunityLineItem = new OpportunityLineItem();
            cloneOpportunityLineItem.PricebookEntryId = obj_pricebookEntry.Id;
            cloneOpportunityLineItem.OpportunityId = clone_Oppertunity.Id;
            cloneOpportunityLineItem.Quantity = 10;
            cloneOpportunityLineItem.UnitPrice = 1;
            insert cloneOpportunityLineItem;
            system.debug(cloneOpportunityLineItem);
            
            //Checking Status
            System.debug('Successfully Created Opportunity and Oppotunity line item & all the clone of Opportunity and clone of Oppotunity line item   ');
            
        }catch(Exception e){
            System.debug('Error occurs-->'+e.getMessage()+'-->On line Number -->'+e.getLineNumber());
        }
       
        
    }
}

If you find your Solution then mark this as the best answer.
Thank you!
Regards,
Suraj Tripathi 
Mohit Kashyap 14Mohit Kashyap 14
In your code you are taking all the standard object. But my requirement is when I clone a Opportunity then all the related list should be cloned including the custom objects.
I ran your code but still the same error is coming up.