+ Start a Discussion
Firas Taamallah 18Firas Taamallah 18 

Trigger - How to link child record to parent record?

Hi ,
  • My parent objet Test_Campaign__c have lookup Core_Campaign_Model__c on itself.
  • My child object have lookup field Test_Campaign__c to Test_Campaign__c 
My need is to link child records to parent every time new parent is created with Core_Campaign_Model__c   equal child.Test_Campaign__c

Here's my trigger : 
 
trigger TRG_LinkCampaing on Test_Campaign__c (after insert,after update) {

    
    set<Id> ParID = new set<Id>();
    
    
    for(Test_Campaign__c TestCamp : Trigger.new)
    {
      ParID.add(TestCamp.Core_Campaign_Model__c);
    }
    List<Test_Scenario__c> TS = [Select Id , Test_Campaign__c from Test_Scenario__c where  Test_Campaign__c In :ParID ];
    List<Test_Campaign__c> TC = [Select Id,name from Test_Campaign__c where Id = : Trigger.new];

     List<Test_Scenario__c> TestCampaToUpdate = new List<Test_Scenario__c>();
    for (Test_Scenario__c Testsc : TS){
         
       Testsc.Test_Campaign__c = TC[0].Id;
        TestCampaToUpdate.add(Testsc);
        
    }
    update TestCampaToUpdate;
}

This trigger works fine but only one time due to TC[0].Id , how can i avoid this?
Daniel AhlDaniel Ahl

Hello Firas,

So you are looking for a trigger that works if you insert more than one Test_Campaign__c at a time?

 

trigger TRG_LinkCampaing on Test_Campaign__c (after insert,after update) {
    
    
    set<Id> ParID = new set<Id>();    
    for(Test_Campaign__c TestCamp : Trigger.new)
    {
        ParID.add(TestCamp.Core_Campaign_Model__c);
    }
    
    List<Test_Scenario__c> TS = [Select Id , Test_Campaign__c from Test_Scenario__c where  Test_Campaign__c In :ParID ];
    //List<Test_Campaign__c> TCs = [Select Id,name from Test_Campaign__c where Id = : Trigger.new];
    
    List<Test_Scenario__c> TestCampaToUpdate = new List<Test_Scenario__c>();
    
    for(Test_Campaign__c tc : Trigger.new){
        for (Test_Scenario__c Testsc : TS){ 
            if(Testsc.Test_Campaign__c == tc.Core_Campaign_Model__c){
                Testsc.Test_Campaign__c = TC[0].Id;
                TestCampaToUpdate.add(Testsc);
            }
        }
    }
    update TestCampaToUpdate;
}
This might work, as long as I didn't misunderstand your goal.
Firas Taamallah 18Firas Taamallah 18
Well i want to clone records from child to parent , not to cut records and insert them into parent
Daniel AhlDaniel Ahl
The code I provided does relates the scenario-records to the new parent, just as your trigger did, but for each inserted/updated Test_Campaign__c record
Firas Taamallah 18Firas Taamallah 18
any way, your method doesn't work 
Daniel AhlDaniel Ahl

What exactly is it that you want to do then?

From what I can see on your code is that
1. A Test_Campaign__c record is created/updated.
2. You fetch all the Test_Scenarios__c that has the same Test_Campaign__c record as the created/updated Test_Campaign__c records Core_Campaign_Model__c value.
3. Change the Test_Scenarios__c.Test_Campaign__c field to the created/updated records Id.
4. Update all the Test_Scenarios__c that you fetched in step 2.

The trigger I sent you does the same thing, but bulkified to work for multiple record-creations/updates.
If this isn't how it's suppposed to work then you have to provide more information as to what your real goal is.

Firas Taamallah 18Firas Taamallah 18
trigger TRG_LinkCampaing on Test_Campaign__c (after insert,after update) {

    public static Boolean stopRecursion = false;
    set<Id> ParID = new set<Id>();
    
    
    for(Test_Campaign__c TestCamp : Trigger.new)
    {
      ParID.add(TestCamp.Core_Campaign_Model__c);
    }
    List<Test_Scenario__c> TS = [Select Id , Test_Campaign__c from Test_Scenario__c where  Test_Campaign__c In :ParID ];
    List<Test_Campaign__c> TC = [Select Id,name from Test_Campaign__c where Id = : Trigger.new];

     List<Test_Scenario__c> TestScenarioToUpdate = new List<Test_Scenario__c>();
      List<Test_Campaign__c> TestCampToUpdate = new List<Test_Campaign__c>();
    try{
    for(Test_Campaign__c tc : Trigger.new){
        for (Test_Scenario__c Testsc : TS){ 
            if(Testsc.Test_Campaign__c == tc.Core_Campaign_Model__c){
                 if( !stopRecursion ) {
                 Testsc.Test_Campaign__c = TC.Id;
                TestCampToUpdate.add(tc.clone(false, false, false, false));
                TestScenarioToUpdate.add(Testsc);
                      stopRecursion = true;
                 }
            }
        }
    }
    if(TestCampToUpdate.size()>0){
    insert TestCampToUpdate;
    }
    update TestScenarioToUpdate;
    }catch(Exception e){}
}

This code doesn't work ..
Daniel AhlDaniel Ahl

Hi Firas,
Do you get any errors, is that why it doesn't work? 
Or is it that it's not doing what you are expecting it to do?