I have a trigger that when an opportunity stage name is closed won, it creates a new opportunity. My question is how do I go about linking opportunities together with an actual link back to the old opportunity.

At the moment opportunity name field has the same name as the closed won opp, but I would like to know how to code the link as well. I have tried getting opp id and name but that just gives me errors.

This is a code snippet where the fields are set.

// Create opportunity
            Opportunity newOpp = new Opportunity(
                Name = opp.get(opp.id),
                RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Dealership').getRecordTypeId(),
                AccountId = dealershipId,
                CloseDate = Date.today(),
                StageName = 'Group Contract Signed'

1 Answer 1


This is a draft that I've put together to demonstrate how you can leverage Lists a) to get all the Opportunities that match the logic and the trigger execution contexts that you care about and b) to create the Opportunities that should then be created as a result.

Best practice would leverage a helper class but the logic below hopefully will be helpful to you.

You can use a design pattern like this:

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

        /* Create a List which stores all the Opportunities that meet the trigger execution context and are Closed Won */ 
        List<Opportunity> oppList = new List<Opportunity>();    

        / * Loop thru all the Opportunities that meet the trigger execution context and add them to the oppList */
        for (Opportunity opp : Trigger.new){
            if(opp.StageName == 'Closed Won'){

        if (oppList.size() > 0) { // control flow 

            /* Create a List which stores all the Opportunities that will be created */
            List<Opportunity> oppListToCreate = new List<Opportunity>();

            / * Loop thru all these Opportunities 
            for (Opportunity opp : oppList){

             Opportunity newOpp = new Opportunity(
             RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().getRecordTypeId();
             Name = opp.Name + ' - Group Contract Signed version.';
             AccountId = opp.AccountId;
             CloseDate = Date.today();
             StageName = 'Group Contract Signed';
             Related_Opp__c = opp.Id); // the custom lookup field to the Opportunity SObject which links the newly created Opp to the prior opp


             if (oppListToCreate.size()>0){ // control flow
                insert oppListToCreate;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .