+ Start a Discussion
SFDC-DMGSFDC-DMG 

Opportunity Clone with added Line Item

Hi,

 

I'm looking to clone an Opportunity and create a line item at the same time.

 

 

Ideally, I would want to clone the Opportunity with products, make an update on the line items indicating that they were a part of the original Opportunity, and then add an additional line item to credit the total amount of the original Opp.

 

 

My goal is to clone the Oppportunity and create an additional product line item that is basically a credit for the original opportunity's amount. Can anyone help provide me some insight on this? I am new to coding, and I'm getting stuck on what code I use to clone an Opportunity. I think the code below will just update the fields that I specified rather than just clone everythign that's already existing.

 

Can someone please help me get back on the correct path for this or let me know if it is possible?

 

Thank you!!

 

trigger RenewalUpgrade on Opportunity (after insert, after update){

 

List<Opportunity> opptsToInsert = new List<Opportunity>();

        Opportunity newOppt = new Opportunity();

       

        for(Opportunity oppt : [Select RecordTypeId, Name, Contact_Name__c, Email__c, AccountId, Amount

From Opportunity where Id in :mapRepSitesOpportunities.values()])

        {

            newOppt.Name =  oppt.Name + ' Renewal Upgrade';

            newOppt.AccountId = oppt.AccountId;

            newOppt.Contact_Name__c = oppt.Contact_Name__c;

            newOppt.RecordTypeId =  '01240000000USG';                                                     

            newOppt.CloseDate = newOpptCloseDate;

            newOppt.StageName = 'Opportunity'; 

            newOppt.Type = 'Subscription Renewal Upgrade';

            newOppt.Renewal_Upgrade__c = oppt.AccountId ;

            newOppt.Addressable_Renewal_Amount__c = oppt.Amount; 

            

           

            for (OpportunityLineItem lineItem :oppt.OpportunityLineItems) {

                newOppt.Description = lineItem.PricebookEntry.Name;

            }

  insert(opptsToInsert);

           

        } {

 

for(Opportunity opptInserted : [Select Id, Amount, Description from Opportunity where Id in :opptsToInsert])

            {      

                newOpptLineItem.OpportunityId = opptInserted.Id;

                newOpptLineItem.Quantity = 1;  

                newOpptLineItem.UnitPrice = -opptInserted.Amount;

                newOpptLineItem.Product = '01t40000003TkIJ';

                

                                             

         

            insert(opptLineItemsToInsert);

        }

}

 

        }