+ Start a Discussion

Trigger for creating junction object record depending on lookup field selection

We have three lookup fields on our Account page to a custom object we created called 'Partners':








We also have a junction object called 'Account/Partner Relationship' that includes a lookup field to Accounts and a lookup field to Partners.


Basically, if someone populates one of those three look-up fields I listed at the top, we want a new Account/Partner Relationship record to be created with the Account being populated in the account look-up field and partner chosen in the partner look-up field.


I'm assuming We need a trigger to do this, but I have zero experience with APEX code. Any help would be greatly appreciated. Let me know if you need more information from me.





Yes, this can be done using a trigger. But what if there is an update and the user changes the existing value of any of the lookup fields. Do you want the Account/Partner Relationship record to be deleted and a new one created based on the new lookup value?

What if the lookup value is simply removed. Do you want the Account/Partner Relationship record to be deleted as well.



Satish Kumar

Hey Satish,

For the first scenario, I would like the old record to be deleted and a new record to be created.

For the second scenario, I would like the record to be deleted.

Let me know if you need anymore info! Thanks.

trigger trigAccount_afterinsert on Account (after insert) {
for (Account a : Trigger.new) {

if(Email_Provider__c != null)
Account_Parent_Relationship obj = new Account_Parent_Relationship();
obj.PartnerId= a.Email_Provider__c;
obj.AccountId= a.Id;
insert obj;
if(Social_Media_Management__c ! = null)
Account_Parent_Relationship obj1 = new Account_Parent_Relationship();
obj1.PartnerId= a.Social_Media_Management__c;
obj1.AccountId= a.Id;
insert ob1j;
if(Facebook_Ad_Platform__c != null)
Account_Parent_Relationship obj2 = new Account_Parent_Relationship();
obj2.PartnerId= a.Facebook_Ad_Platform__c;
obj2.AccountId= a.Id;
insert obj2;



This is only for insert you can use before delete and after delete and read about merge trigger

Thanks so much rbansal!

However, I don't have any experience implementing a trigger. Don't I have to create a test class or something? What's the first step?

You just need to create a new trigger by going through developer console. A test class is not necessary but recommended as its mandatory to have a test class when creating a managed package.

What do I do once I'm in the developer console? I'm confused where to put the code.
Hey rbansal, can you point me in the right direction in the developer console? Where do I put the code? Sorry for being such a newbie.
Thomas DvornikThomas Dvornik

To create a trigger, go to <You name> -> Developer Console. Once the Developer Console is loaded, go to File -> New -> Trigger. Enter in the name trigAccount_afterinsert, and the sobject Account. Then when the tab opens, enter in the code that rbansal posted and go to File -> Save.


At this point, if you want to see your trigger in action, go to Debug -> Open Execute Anonymous Window (or Ctrl + E) and create a new account. i.e. 


insert new Account(name='Smith', Email_Provider__c = 'Provider');

 I haven't tried that, so you will probably need to modify the fields. Then you can go to the Query Editor and query for your new sobjects.


SELECT ParentId, AccountID FROM Account_Parent_Relationship