0

I have a custom object called Account Affiliations which is a junction object between two accounts. Below is the schema of the object.

Object Name: Account_Affiliations__c Custom fields: 1) Account to (Account_to__c)--lookup relationship 2) Account From (Account_from__c)-- lookup relationship 3) Department (department__c) --picklist

lets assume we have two accounts, Account A, Account B. Lets create an account affiliation record between these two. Every time a account_affiliation__c record is inserted, we have to create another affiliation record and have to swap the account to and account from Ids in the new record.

Record 1: Account From: Account A, Account To: Account B, Department: manufacturing

Record 2: Account From: Account B, Account To: Account A, Department: manufacturing (this should be automatically created)

how to accomplish this using a trigger to handle insert, update and delete? any pointers will be highly appreciated.

1
  • 1
    and what have you tried? normal forum etiquette is to demonstrate some effort and then you'll get lots of responses
    – cropredy
    Feb 10, 2016 at 6:05

1 Answer 1

0

For this you can create one more field by the name "Associated Affiliations" which is the lookup to "Account Affiliation Record". Now you can use the below Trigger and associated class to insert new record whenever you create new "Account Affiliations" record.

Trigger

Trigger AccAffTrigger on Account_Affiliations__c (After Insert) 
{
    IF(Trigger.IsAfter)
    {
        IF(Trigger.IsInsert)
        {
            For(Account_Affiliations__c AA : Trigger.New)
            {
                IF(AA.Associated_Affiliations__c == NULL) //This condition is imp coz else the trigger will go in Infinite loop.
                {
                    AccountAffiliationHelper.InsertNewAccountAff(Trigger.New);
                }
            }
        }
    }
}

Class

Public Class AccountAffiliationHelper
{
    Public Static Void InsertNewAccountAff(List<Account_Affiliations__c> AccAffIds)
    {   
        List<Account_Affiliations__c> AccAffList = New List<Account_Affiliations__c>();

        For(Account_Affiliations__c AA : AccAffIds)
        {
            Account_Affiliations__c AAToInsert = New Account_Affiliations__c();

            AAToInsert.Account_From__c = AA.Account_To__c;
            AAToInsert.Account_To__c = AA.Account_From__c;
            AAToInsert.Associated_Affiliations__c = AA.Id;
            AAToInsert.Department__c = AA.Department__c;
            AccAffList.Add(AAToInsert);
        }
        Insert AccAffList;
    }
}

This is only for the record insertion. In the same way you can do for update and delete. Just add 2 new methods in the class and call them in your trigger. For Update and delete run your trigger over the loop of your affiliations record and find the associated "Affiliation record". Then Update/Delete it.

Hope this will help you.

1
  • Thanks Ruchi for the sample code. i was able to build out the trigger. Feb 11, 2016 at 17:06

You must log in to answer this question.

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