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
SFDC12SFDC12 

clone scenario

Hi everyone Iam very new to salesforce trying to clone opportunity and opportunityLineitem but unable to see the result correctly,Below is the code which i have tried..

class:
public class cloneoppandopplineitem {
    public static void oppmethod(List<id>oppid){
    opportunity  opp=[select id,name,stageName,closeDate from opportunity where id=:oppid];
        opportunity oppinse=opp.clone(false);
        system.debug('1-->'+oppinse);
        oppinse.StageName='prospecting';
        insert oppinse;
        system.debug('2-->'+oppinse);
        
        opportunityLineItem oli=[select Product2.name,opportunityId, Product2Id,Quantity from OpportunityLineItem where opportunityId=:oppid];
        system.debug('3-->'+oli);
        opportunityLineItem ol=oli.clone(false);
        ol.OpportunityId=oppinse.id;
        insert ol;
        system.debug('4-->'+ol);
    }
}
Best Answer chosen by SFDC12
AbhinavAbhinav (Salesforce Developers) 
Have you checked below link? it has answer with similar requirement .

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

If it helps mark it as best.

Thanks!
 

All Answers

mukesh guptamukesh gupta
Hi ,

You can use standard button "Clone With Related", and you can choose Opprtunity Line Item from There.

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
SFDC12SFDC12
Thanks for Your response, but I need to write using apex..
AbhinavAbhinav (Salesforce Developers) 
Have you checked below link? it has answer with similar requirement .

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

If it helps mark it as best.

Thanks!
 
This was selected as the best answer
SFDC12SFDC12
Thank you for your response .