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
Kunal Purohit 4Kunal Purohit 4 

How to apply validation rule in junction object?

I have junction object "Author-Research Paper". This object has two master detail relationship with objects "Authors" and "Research Paper".
Now,there must not be more than 5 Authors for single research paper. How to write validtion rule for it? 
AbhishekAbhishek (Salesforce Developers) 
You'll need to do this in Apex as follows:


1. Create an Apex Trigger on before insert of your junction object

2. In your trigger, iterate through the records inserted and for SELECT back the id's you're checking

3. If you find a match for the Case and Issue Id it won't be the current record (as it hasn't been written yet in a before insert trigger), all you need to then do is call the .addError method on the record


Depending on your objects it should look something like this:

trigger trg_bi_caseissue on CaseIssue__c (before insert) {<br>    <br>    <br>    // Iterate through the list of new records<br>    for (CaseIssue__c ci : trigger.new) {<br>        <br>        // We only care if there is one record<br>        List<CaseIssue__c> resultList = [SELECT id, Name FROM CaseIssue__c <br>                         WHERE  Case__c = :ci.Case__c<br>                         AND    Issue__c = :ci.Issue__c];<br>         <br>         // Raise the error back if any records found         <br>         if (!resultList.isEmpty()) {<br>             System.debug('Found another copy ' + resultList[0].id + '  name is ' + resultList[0].Name);<br>             ci.addError('Duplicate record, a Case Issue already exists for that combination');<br>         }<br>    }<br><br>}<br>

For further reference, you can check this blog (https://developer.salesforce.com/forums/?id=906F000000091mrIAA)too.

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.
Kunal Purohit 4Kunal Purohit 4
Not Working... It prevents duplicate entry... My concern is that A single research paper should not have more than 5 Authors
Kunal Purohit 4Kunal Purohit 4
Hi. I get it done through validation rule. On Research paper object, i creted one rollup summary,named Author Count.. After that created validation rule "Author_Count__c>5",Error Message "Cannot be more than 5 Authors for same Research paper".
Jonye KeeJonye Kee

They must check it thoroughly for compliance with the requirements and accept or send it back for additional review.

Source: research paper writers (https://researchpaperwriter.org/)

kal rosekal rose
I had faced some similar problems in a research for Kalahari Rose Skincare. But somehow they had saved it.