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
SaltinSaltin 

Junction Object Validation Rule guidance

Hi


I have a junction object called CaseIssue whith two master detail relationships. On one side is Case (Case_Number__c_) , on the other side is Issue (Issue_id__c).  This setup allows us to establish a many to many relationship between cases and issues. Pretty standard stuff.

 

I would like to ensure however, that we are not linking the same Issue record to a single case record more than once (and vice versa not linking the same Case record to a single Issue record more than once).


The first thing that ocurred to me was VLOOKUP, but I am unsure if this is right? Any help would be greatly appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
EnthEnth

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) {


// Iterate through the list of new records
for (CaseIssue__c ci : trigger.new) {

// We only care if there is one record
List<CaseIssue__c> resultList = [SELECT id, Name FROM CaseIssue__c
WHERE Case__c = :ci.Case__c
AND Issue__c = :ci.Issue__c];

// Raise the error back if any records found
if (!resultList.isEmpty()) {
System.debug('Found another copy ' + resultList[0].id + ' name is ' + resultList[0].Name);
ci.addError('Duplicate record, a Case Issue already exists for that combination');
}
}

}

 


 

Message Edited by Enth on 03-20-2010 06:39 PM

All Answers

Steve :-/Steve :-/
If you've got Master-Detail Relationships you might be able to do a VR based on a Roll-Up Summary field
SaltinSaltin

Hi Stevemo,


Thanks for the suggestion. I am unclear how to use this to my advantage however. It is easy enough to create a rollup summary on the Case object to count the total number of CaseIssues attached, however how can I use a raw count like that to determine uniqueness?

 

I have a junction object CaseIssue with 3 fields, Name, Case_ID and Issue ID.

 

I need to be sure that when I save a CaseIssue Record with Case_ID XYZ and Issue ID ABC that for every other CaseIssue Record with Case_ID XYZ, the Issue ID Value is not ABC. If it is, I need an error.


Sorry if I seem a little unclear or slow on the uptake, I'm a Salesforce rookie.


Thanks again.

Steve :-/Steve :-/

Hmmmmm...  you're right, this is gonna be a tougher nut to crack than I thought.  

 

Lemme take another look at it  

EnthEnth

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) {


// Iterate through the list of new records
for (CaseIssue__c ci : trigger.new) {

// We only care if there is one record
List<CaseIssue__c> resultList = [SELECT id, Name FROM CaseIssue__c
WHERE Case__c = :ci.Case__c
AND Issue__c = :ci.Issue__c];

// Raise the error back if any records found
if (!resultList.isEmpty()) {
System.debug('Found another copy ' + resultList[0].id + ' name is ' + resultList[0].Name);
ci.addError('Duplicate record, a Case Issue already exists for that combination');
}
}

}

 


 

Message Edited by Enth on 03-20-2010 06:39 PM
This was selected as the best answer
EnthEnth
BTW, you may also want the trigger to fire on an undelete of a CaseIssue.
SaltinSaltin

Thanks a million Enth, this is exactly what I need. I appreciate the help and am busy dissecting the example to learn something from it.