Hey guys, today in this post we are going to learn about How can we prevent duplicate name insert on custom object in Salesforce.
Real time scenarios:- Write a trigger on custom object When a Object (childObjTrigger__c) is being created, if a record exists with the same EmployeeName (EmployeeName__c), the trigger should throw an error.
Create a custom object with custom field name as EmployeeName
Custom Object:- childObjTrigger__c >> custom field:- EmployeeName__c (Text
type)
- Don’t forget to check out:- Trigger to update count of child records with custom field of parent object Click Here For More Information
Files we used in this post example:-
childTriggerhandler.apxt | Apex Class Trigger | It is used for prevent duplicate record creation with same name |
childObjHandler.apxc | Apex Class Handler | It is call into cildTriggerhandler.apxt file. |
Custom Child Object:- childObjTrigger__c Custom Field:- EmployeeName__c |
Custom Child Object with Custom Field | It is custom object with custom field used for display an error if same name being inserted. |
You can download file directly from github by click here
Final Output
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
Step 1:- Apex Class Trigger : childTriggerhandler.apxt
From Developer Console >> File >> New >> Apex Trigger
childTriggerhandler.apxt [Apex Class Trigger]
TRIGGER childObjTriggerHandler ON childObjTrigger__c (BEFORE INSERT, after INSERT) {
IF(TRIGGER.isBefore){
IF(TRIGGER.isInsert){
system.debug('I am inside before insert');
childObjHandler.childDuplicate(TRIGGER.new);
}
}
ELSE IF(TRIGGER.isAfter){
IF(TRIGGER.isInsert){
//system.debug('I am inside after insert');
system.debug('Record inserted successfully');
}
}
}
Step 2:- Apex Class : childObjHandler.apxc
From Developer Console >> File >> New >> Apex Class
childObjHandler.apxc [Apex Class Handler]
public class childObjHandler {
//Do NOT allow duplicate Employee name
public static void childDuplicate(List<childObjTrigger__c> dupChildRec){
Set<String> empExtStr = NEW Set<String>();
List<childObjTrigger__c> empList = [SELECT Id, Name, EmployeeName__c, childLookup__c, Status__c FROM childObjTrigger__c WHERE EmployeeName__c !=NULL];
FOR(childObjTrigger__c empStr:empList){
empExtStr.add(empStr.EmployeeName__c);
}
FOR(childObjTrigger__c childTr:dupChildRec){
IF(empExtStr.contains(childTr.EmployeeName__c)){
childTr.EmployeeName__c.addError('Do not allow duplicate');
}
}
}
}
Further post that would you like to learn in Salesforce
What is Apex trigger in Salesforce?
Apex triggers enable you to perform custom actions before or after events to records in Salesforce, such as insertions, updates, or deletions. Just like database systems support triggers, Apex provides trigger support for managing records.
What is the use of Apex triggers?
Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions. A trigger is Apex code that executes before or after the following types of operations: insert. update.
What is the difference between Apex and trigger in Salesforce?
Classes consist of other classes, user-defined methods, variables, exception types, and static initialization code A trigger is Apex code that executes before or after specific data manipulation language (DML) events occur, such as before object records are inserted into the database, or after records have been deleted.
Related Topics | You May Also Like
Our Free Courses →
👉 Get Free Course →
📌 Salesforce Administrators 📌 Salesforce Lightning Flow Builder 📌 Salesforce Record Trigger Flow Builder |
👉 Get Free Course →
📌 Aura Lightning Framework 📌 Lightning Web Component (LWC) 📌 Rest APIs Integration |