function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jas SalesforceJas Salesforce 

Duplication rule for Junction object

Hi Team, 
I got a junction object on Account for Account-Account relationship with one master-details field ( Account_to_c) and lookup field (Account_from) which is linking accounts to multiple accounts. 
i need to add duplication rule to avoid mulitple entries in 'Account to' and 'account from' field. Rule should check if Acount A's relationship exist in either Account to or Account from lookups then show a error when creating duplicate entry. 

Standard duplicate rules and workflow rule options are not working.

Can someone please suggest a way or if it can achieve via trigger then please can help me to write trigger as  still a beginner with writing codes. 
CharuDuttCharuDutt
Hii Jas 
Try Below Code
trigger PreventDuplication on Account (before insert,before Update) {
Set <Id> AccFromId = new Set<Id>();
Set <Id> AccToId = new Set<Id>(); 
if(trigger.IsBefore &&(Trigger.IsInsert || Trigger.IsUpdate)){
    for (Account Acc:trigger.new) {
        AccToId.add(Acc.Account_To_c);
        AccToId.add(Acc.Account_From_c);
    }
}
    List <Account> lstAcc = [SELECT Account_To_c,Account_From_c FROM Account WHERE Account_To_c IN :AccToId  AND Account_From_c  in :AccFromId ];

    for (Account Acc2:trigger.new) {
        If (lstAcc.size() > 0) {
            
          Acc2.Account_To_c.adderror( 'Duplicate Account Found' );
          Acc2.Account_From_c.adderror( 'Duplicate Account Found' );
            
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
Jas SalesforceJas Salesforce
Hi Charu, 
Thanks so much for the help, but it is firing up. code is as below, has no errors. Accounts relationship is a custom object has two fields Accounts to and account from added on accounts as related list so can add multiple account relationships. User can add new relationship from 'Account to' relationship related list and that will show up on reciprocal account in 'Account from' related list. Would like system should check duplicates in Account to and Account From related list when adding new relationship from 'Account to'  
---------------------------------------------------------------------------------------------------------------------------------------------------------
trigger PreventDuplicationonAccountrelationship on Account_Relationship__c  (before insert,before Update) {
Set <Id> AccFromId = new Set<Id>();
Set <Id> AccToId = new Set<Id>(); 
if(trigger.IsBefore &&(Trigger.IsInsert || Trigger.IsUpdate)){
    for (Account_Relationship__c Acc:trigger.new) {
        AccToId.add(Acc.Account_To__c);
        AcctoId.add(Acc.Account_From__c);
    }
}
    List <Account_Relationship__c> lstAcc = [SELECT Account_To__c,Account_From__c FROM Account_Relationship__c WHERE Account_To__c IN :AccToId  AND Account_From__c  in :AccFromId ];

    for (Account_Relationship__c Acc2:trigger.new) {
        If (lstAcc.size() > 0) {
            
          Acc2.Account_To__c.adderror( 'Duplicate Account Found' );
          Acc2.Account_From__c.adderror( 'Duplicate Account Found' );
            
        }
    }
}
----------------------------------------------------------------------------------------------------------------------------------
Object view, as you can see that i can add multiple accounts from 'Relationship to' related list and see same account added from 'Relationship from' list too. 
 
Jas SalesforceJas Salesforce
User-added image