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 on Junction Object

Hi Team, (this is my original question where Charu helped me but the code is not working, so adding more details for help please)
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.


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. 

User-added image
 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Jas,

>> https://salesforce.stackexchange.com/questions/12755/how-to-apply-uniqueness-on-junction-object

I was checking the possibilities and one was mentioned in the above link can you try checking the mentioned way once?

I am adding the best answer mentioned in the above link for quick reference:
 
Create a text field (External Id, Unique) on the junction object.

Create a workflow on C to fire Everytime the record is created or edited (to account for cases where junction object records are reparented) to set this field as the concatenation of the Ids of A and B records that the C record is relating.

This workflow trying to set the composite primary key field will generate an error if a duplicate record is being inserted.

You can add to the composite key to include any other uniqueness criteria such as relationship type if you have that notion.

The workflow will fire only for new records created, you will need to retrospectively populate for already existing records possibly by extracting and concatenation via data loader, or simply do a phantom update on the junction records and that should cause the workflow to fire and set the key. However this could also throw some errors for any existing duplicates which you may need to resolve manually.

Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.  

Thanks.
Jas SalesforceJas Salesforce
Hi @Anutej, thanks for the reply. i have already added this options, and works all fine. but it solving half of my problem. It only checks duplicates for 'relationship to'  but i want to check duplicate value in 'relationship from' as well. cause when relationship have been populated from 'Relationship to' account than it show up in 'relationship from' in reciprocal account. Hope this makes sense. thats why thought will try trigger if it can help.