I am completely stuck on how to get started with this. My application works like this: I create an Opportunity, when the status is set to closed won, a contract is created and a renewal policy is created. When the renewal policy is set to closed won, a new contract is created (along with another renewal policy). So this is quite a simple process.

The problem that is not so simple is, Contract has a Junction object to address:

Contract -------- ContractAddressJunction -------------- Address

When the new contract is created from the renewal opportunity, I want to attach the same addresses to the new Contract.

I really don't know how to get started, I am already failing at the query.

Any help is appreciated.

1 Answer 1


Opp1 gets set to closed won

Contract1 and Opp2 (renewal policy) get created <-- Opp2 should have a lookup to Contract1

Opp2 gets set to closed won <-- your Opp trigger should check if the Opps in trigger.new just got set to Closed Won; You should then scope out all Contracts based on the lookup I mentioned

Then you can select all the junction objects for those contracts

Now create your Contract2s and insert

Now you need to loop round the scoped junction objects and clone each one but change the Contract lookup (or master-detail) to the correct Contract2 (if you're having trouble with this bit I will put some sample code together, but give it a go first :) )

Update - sample code

Hopefully this will get you started! It's untested but should show the general concept. Notice the new Contract also relate to the Opp that caused them to be created.

Set<Id> conIds = new Set<Id>();
List<Contract> consToIns = new List<Contract>();
List<Opportunity> scopedOpps = new List<Opportunity>();

for (Opportunity opp : trigger.new) {
    if (null != opp.Contract__c) {

        Contract con = new Contract();
        con.Opportunity__c = opp.Id;

Map<Id, Contract> consWithJunc = new Map<Id, Contract>([SELECT Id,
                                      (SELECT Id,
                                       FROM Con_Add_Juncs__r)
                               FROM Contract
                               WHERE Id IN :conIds]);

insert consToIns;

Map<Id, Contract> newConsByOppId = new Map<Id, Contract>();
for (Contract con : consToIns) {
    newConsByOppId.put(con.Opporunity__c, con);

List<Con_Add_Juncs__r> newConAdds = new List<Con_Add_Juncs__r>();

for (Opportunity opp : scopedOpps) {
    Contract con = consWithJunc.get(opp.Contract__c);

    for (Con_Add_Juncs__r conAdd : con.Con_Add_Juncs__r) {
        Con_Add_Juncs__c newConAdd = conAdd.clone();
        newConAdd.Contract__c = newConsByOppId.get(opp.Id).Id;

insert newConAdds;
  • I have already implemented the first bit. It works fine. The thing I am having problems with is writing the query to get the addresses for contract 1 and then attaching it to contract 2.
    – Lily
    Commented Dec 11, 2014 at 12:05
  • Ah ok, you did say you were stuck getting started ;) When I get a chance I'll write you some sample code
    – Force Hero
    Commented Dec 11, 2014 at 12:14
  • Do you think you could get me started on the query?
    – Lily
    Commented Dec 11, 2014 at 12:16
  • Hello, I have just tried this and it says that it doesn't know the relationship Con_Add_Juncs__r. I obviously renamed it to the correct junction object, so not sure what's wrong. Thanks.
    – Lily
    Commented Dec 12, 2014 at 9:21
  • You need to use the Salesforce workbench (workbench.developerforce.com/login.php) or the schema in eclipse to find out the relationship name on the Contract object for the junction object
    – Force Hero
    Commented Dec 12, 2014 at 9:27

You must log in to answer this question.

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