You need to sign in to do that
Don't have an account?
Firas Taamallah 18
Trigger - How to link child record to parent record?
Hi ,
Here's my trigger :
This trigger works fine but only one time due to TC[0].Id , how can i avoid this?
- 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
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?
Hello Firas,
This might work, as long as I didn't misunderstand your goal.So you are looking for a trigger that works if you insert more than one Test_Campaign__c at a time?
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.
This code doesn't work ..
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?