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
chubsubchubsub 

wrapper class help - checking for duplicates before inserting

I'm using a wrapper class on the Case object so that users can select multiple users to "tie" them to a case.  Upon selection, these additional users are inserted into the Case_Associate__c object, which is a detail to the Case.  

 

Here is a link to the full code: http://boards.developerforce.com/t5/Apex-Code-Development/Test-Method-to-cover-Wrapper-Class-only-covering-79/m-p/364981

 

How can I check to see if a record has already been created in the Case_Assoicate object in my Apex class, before inserting, to avoid duplicate Case Associate records that include the same case ID and user ID?  Below is the code snippet I need to expand on:

 

Thanks for any help!

 

public PageReference processSelected() {

 

//We create a new list of Users that we be populated only with Users if they are selected


List<User> selectedUsers = new List<User>();
List<Case_Associate__c> caseAss = new List<Case_Associate__c>();  //here is where I list out the records to set it up to check for dupes before inserting

 

//We will cycle through our list of cUsers and will check to see if the selected property is set to true, if it is we add the User to the selectedUsers list


for(cUser cUse: getUsers()) { 
if(cUse.selected == true && User.Id != Case_Associate__c.User.Id) {  // check for duplicates on the Case Associate object before adding the Users to the selectedUsers list
selectedUsers.add(cUse.use);
}
}

Best Answer chosen by Admin (Salesforce Developers) 
(e)(e)

For instances like this I create a unique, case-sensitive, external id field that is a composite key of the two tables I care about. In this situation, it would be User.Id+Case.Id (or Case.Id+User.Id), and I use a workflow rule on every update to set the composite key. Then, you can do an upsert on your composite key without worries that you will get duplicates. i think workflow rules only use the 15 character case-sensitive id... so keep that in mind in your apex code for upserting. And make sure to run the workflow rule on every record before putting the apex code into production.

All Answers

UVUV

I would suggest you to write a trigger to prevent duplicates records from insertion.

http://www.salesforce.com/docs/developer/cookbook/Content/apex_dedupe.htm

(e)(e)

For instances like this I create a unique, case-sensitive, external id field that is a composite key of the two tables I care about. In this situation, it would be User.Id+Case.Id (or Case.Id+User.Id), and I use a workflow rule on every update to set the composite key. Then, you can do an upsert on your composite key without worries that you will get duplicates. i think workflow rules only use the 15 character case-sensitive id... so keep that in mind in your apex code for upserting. And make sure to run the workflow rule on every record before putting the apex code into production.

This was selected as the best answer
chubsubchubsub

(e), I tired your way and it works pretty well.

 

I was wondering if there is a way to display a custom error message instead of the visualforce error message that appears when a duplicate is selected. 

 

This is on a brand new object, so I wouldnt have to worry about updating exisiting records.

 

This is a great trick by the way, very clever.

(e)(e)
I would look to the adderror methods on sObjects. i haven't tried it, but I would think that would pop into the interface when you adderror within a insert/update trigger on your record.
Amit Singh1989Amit Singh1989

i had gone through this link

 

http://www.salesforce.com/docs/developer/cookbook/Content/apex_dedupe.htm

 

i have implemented same logic for my custom objects, but i am facing some issues...

 

Job is one of my custom object, i have written trigger to not allow duplicate job name (here i am using Record name as Job name)...

 

trigger is working fine, i have created a job with job name "Job 1",and when i go to create another job with name "Job 1",,, it throws a message....

but with name "job 1" it does not.

 

why???

how to solve this issue?

 

 

Thanks,