• Pax
  • NEWBIE
  • 10 Points
  • Member since 2016
  • XRC Labs

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Hi and thanks in advance for your help! I've been working on an apex trigger for a junction object that has two custom master-detail fields, but I'm stuck and could use some help.

I have created a junction object to create a connection between two Accounts (1st: Account object; 2nd: Connected_Account custom object). Connected_Account__c functions as a one-to-one relationship with Account (every Account has itself as a Connected_Account)

junction object:
Connection__c
- Connection__c.Name : auto number {0}
- Account1__c : master-detail(Account)
- Account2__c : master-detail(Connected_Account__c)

Connected_Account__c
- Connected_Account__c : lookup(Account)

User creates a new Connection__c on the Account Page Layout for Account X:

Connection__c.Name = 1
// Account1 autopopulated, Account1 = X
Account1__c = X
// User selects Account2, Connected_Account = Y
Account2__c = Y

Now, what I'm trying to accomplish is a trigger such that after a User creates Connection__c.Name=1, the trigger creates a second Connection__c.Name=2 where the field values are inverted:

Connection__c.Name = 2
Account1__c = Y
Account2__c = X
 
ConnectionHandler.apxc :

public with sharing class ConnectionHandler {
    public static void CreateNewConnection(List<Connection__c> connects) {
        for(Connection__c c : connects) {
            Connection__c connect = new Connection__c();
            connect.Account1__c = c.Account2__c;
            connect.Account2__c = c.Account1__c;
            connects.add(connect);
        }
        if(connects.size() > 0) {
            insert connects;
        }
    }
}


ConnectionTrigger.apxt :

trigger ConnectionTrigger on Connection__c (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
	if(Trigger.isAfter && Trigger.isInsert) {
        ConnectionHandler.CreateNewConnection(Trigger.New);
    }
}

Any advice and/or suggestions would be greatly appreciated. Thanks again!
  • August 21, 2018
  • Like
  • 0
Hi and thanks in advance for your help! I've been working on an apex trigger for a junction object that has two custom master-detail fields, but I'm stuck and could use some help.

I have created a junction object to create a connection between two Accounts (1st: Account object; 2nd: Connected_Account custom object). Connected_Account__c functions as a one-to-one relationship with Account (every Account has itself as a Connected_Account)

junction object:
Connection__c
- Connection__c.Name : auto number {0}
- Account1__c : master-detail(Account)
- Account2__c : master-detail(Connected_Account__c)

Connected_Account__c
- Connected_Account__c : lookup(Account)

User creates a new Connection__c on the Account Page Layout for Account X:

Connection__c.Name = 1
// Account1 autopopulated, Account1 = X
Account1__c = X
// User selects Account2, Connected_Account = Y
Account2__c = Y

Now, what I'm trying to accomplish is a trigger such that after a User creates Connection__c.Name=1, the trigger creates a second Connection__c.Name=2 where the field values are inverted:

Connection__c.Name = 2
Account1__c = Y
Account2__c = X
 
ConnectionHandler.apxc :

public with sharing class ConnectionHandler {
    public static void CreateNewConnection(List<Connection__c> connects) {
        for(Connection__c c : connects) {
            Connection__c connect = new Connection__c();
            connect.Account1__c = c.Account2__c;
            connect.Account2__c = c.Account1__c;
            connects.add(connect);
        }
        if(connects.size() > 0) {
            insert connects;
        }
    }
}


ConnectionTrigger.apxt :

trigger ConnectionTrigger on Connection__c (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
	if(Trigger.isAfter && Trigger.isInsert) {
        ConnectionHandler.CreateNewConnection(Trigger.New);
    }
}

Any advice and/or suggestions would be greatly appreciated. Thanks again!
  • August 21, 2018
  • Like
  • 0