You need to sign in to do that
Don't have an account?
Uma Prabhakar
trigger to prevent duplicate records on multiple fields in salesforce
Hello Developers
i am new to the apex development, i have been given a task for creating Triggers to prevent duplicate records on multiple fields , i know this is can be achived through Duplicate Rules, but still i want to try with the Apex trigger
Assignedto__c (Lookup(User)
Quater_Year__c(Picklist)
Month__c(Picklist)
i want to create a Duplication rule on these 3 fields,
i refered the below link, but i could not get the correct code
https://www.mstsolutions.com/technical/preventing-duplicate-records-based-on-multiple-fields-in-salesforce/
i am new to the apex development, i have been given a task for creating Triggers to prevent duplicate records on multiple fields , i know this is can be achived through Duplicate Rules, but still i want to try with the Apex trigger
Assignedto__c (Lookup(User)
Quater_Year__c(Picklist)
Month__c(Picklist)
i want to create a Duplication rule on these 3 fields,
i refered the below link, but i could not get the correct code
https://www.mstsolutions.com/technical/preventing-duplicate-records-based-on-multiple-fields-in-salesforce/
Apex Class (Updated!):
All Answers
Yes You are Correct, i tried to write the trigger for only one field ie (Assigned to) user lookup field but still i am not able to write a proper trigger for all 3 fields
Apex Class: Apex Trigger:
I have Never Thought of the Other Possibe Scnerios & Assumptions, And By the Way you have Written such a Beautifull Code that even Newbie like me is able to understand the code line by line, Let me Check in my org and try different assumptions and i will get back to you
Apex Class (Updated!):
The Code is Working perfectly fine, i was wondering if it is possible to find the duplicates for the Specific RecordType, ex i have two record types
1)Contacts and 2)Associated Contacts
Now i want to find the Duplicate in Contact Recordtype itself, how do i do that, i tried to define it by Putting the Contact RecordTypeID of contact that is 012p0000000Nn0ZAAS
Id RecordTypeIdContact = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Contact').getRecordTypeId();
is that possible??
i have modified the code, still its not doing anything, its finding duplication for both record types
i tried the above code and its working fine, How do i write a Test class for the above code
This is what i have come up so far, but it is not covering anything
//Test Setup
//Create 200 test Users
//Create 200 contact records and for each contact to one of the test users through the AssignedTo attribute
//Please note that the reason why we are creating so many contact records is to ensure that the trigger is bulkified
//Insert 200 Contact Records
//Use a system Assert method to ensure that there only exist 200 contact after the insert
//Create another contact that is a duplicate of one of the test contacts you inserted. Don't insert the duplicate contact yet.
//Start testing using: System.Test.startTest(); method
//Use Database.insert(List <sObject>, boolean); to insert the second contact.
//Make sure boolean above is set to true to simulate as if we are using DML
//We are using Database method to retrieve the duplicate error message through its Database results counterpart.
//Pass the second contact into the method we are testing: ContactTriggerHandler.dupContactError(List <Contract> newTrigCon);
//End testing using - System.Test.stopTest(); method
//Evaluate Test Results
//Use System assert method to evaluate that there still only exist 200 contact records and not 201 records
//If the code worked, it should have prevented the second contact from being inserted since it is a duplicate
//Iterate through the Database Results and ensure that the error message received is equivalent to the error message we specified.
Sorry to bother you again and again,but i tried designing a test class from above instruction but i am able to cover only 50%
i removed the user__c in the test class and i run the test and it worked fine, it covers 100% code, i am pasting the final code, plz check and let me know if this is the best pratice to write a test class
@isTest
public class ContactTriggerHandlerTestClass {
static testmethod void Duplication(){
List<Contact> Contacts = new list <Contact>();
Id RecordTypeIdContact = schema.SObjectType.Contact.getRecordTypeInfosByName().get('Contact').getRecordTypeId();
//insert Account
Account testAccount = new Account();
testAccount.Name='DCCL' ;
insert testAccount;
System.debug('Account inserted....'+testAccount);
// insert contact
contact con = new contact();
con.FirstName='Julian';
con.LastName='Rogers';
con.Month__c = 'march';
con.Quater_Year__c = '2020';
con.AccountId = testAccount.id;
con.Email = 'hello123@gmail.com';
con.recordtypeid=RecordTypeIdContact;
Contacts.add(con);
insert con;
System.debug(' First Contact inserted...and Id is .....'+con.id);
// Trying to insert duplicate contact with same User__c, Month__c, Quater_Year__c and recordtype as your are comparing these fields in your trigger.
if(con.id!=NULL){
contact con1=new contact();
con1.firstname='Testing';
con1.lastname='duplicate Contact';
con1.Month__c = 'march';
con1.Quater_Year__c = '2020';
con1.AccountId = testAccount.id;
con1.recordtypeid=RecordTypeIdContact;
System.debug(' Scecond Contact inserted...and Id is .....'+con.id);
try{
insert con1;
}
catch(Exception ex){
Boolean expectedExceptionThrown = ex.getMessage().contains('Duplicate Contact Error! This record already exists.') ? true : false;
System.debug('Error message is '+ex.getMessage());
}
}
}
}
Apex Test Class:
Example:
1.) Account ABC
-ContactA = Kirst Belen
There shouldn't be any contact created within "Account ABC" that has the same record with ContactA. But if we create the same contact on Other Account, then it should be created.