Hi Frnds, please help me... to resolve my problem Avoid Duplicate Name Only on master in Master-Detail relationship Hi, I have two custom object Claim__c(master) and Labour_Cost__c(detail). They have master detail relationship. I want to create a Trigger which prevent duplicate Name(standard field which is created default when we create any custom object) only on master object. but we can multipe record with same Name from detail object and we select it from lookup field (Claim_Name__c) on detail object. plz write a trigger for same. for example: I have a record on Master Object with Name='S1 Claim' . if i create a new record with Name 'S1 Claim' then it will display an error 'record already exists with same name'. But,I can create multiple record with Name 'S1 Claim' from detail object.
To prevent duplicate account creation or lead creation .Lets follow salesforce standard method:-
Lets say prevent duplicate account creation :-
Create a text field :- "say X". Select checkboox unique.********* No need to expose in any page layout. Create a workflow rule :- Name it as "say Y " Created and every time it is edited ---Criteria :Account name is not equal to null .SAVE Action:Field update :--name the field update as "say A" From drop down choose text field "X". In formula editior : Name(NOTE:-as account name api is name ). SAVE .ACTIVATE .
Now it is ready ..
Create account >> Go to account tab in header>>NEW>> in account name put "TEST1"
Create another account with same account name"TEST1".
Hope above method helps you..
*********NOTE :- but many of you are thing what if i create 2nd account with name 'test1'--DO it prevent that time also. So answer is it is as per business requirement : If business wants : name should unique subjective to case insensitive means if TEST1 and test 1 should be treated as same account than check checkbox --->Treat "ABC" and "abc" as duplicate values (case insensitive) while creating text field "X"
If business wants : name should unique subjective to case sensitive means if TEST1 and test 1 should be treated as different account than check checkbox --->Treat "ABC" and "abc" as different values (case sensitive) while creating text field "X".
//get old records for(Account acc:accList){ duplicateMap.put(acc.Name,acc); } //compare old records with records in trigger.new for(Account acc:Trigger.new){ if(duplicateMap.containsKey(acc.Name)){ acc.adderror('Name already exists'); } } }
Try This,
trigger DuplicateLead on lead (before insert,before update)
{
Set<string> lastname= new Set<string>();
for(lead lead : Trigger.new)
{
lastname.add(lead .lastname );
}
List<Lead > duplicateleadList = [Select lastname From Lead where lastname = :lastname];
Set<string > duplicateLeadIds = new Set<string >();
for(lead dup: duplicateleadList )
{
duplicateLeadIds .add(dup.lastname);
}
for(lead s : Trigger.new)
{
if(s.lastname!=null)
{
if(duplicateLeadIds.contains(s.lastname))
{
s.addError('Record already exist with same Name');
}
}
}
}
try this below example
trigger DuplicateRecord on Static_Activity__c (before insert)
{
List<Static_Activity__c > StaActiList = [Select Name, Activity_for_Company__c, User_Name__c from Static_Activity__c ];
for (Static_Activity__c staActnew : Trigger.new)
{
for(Static_Activity__c sa: StaActiList)
{
if(sa.User_Name__c == staActnew.User_Name__c)
{
staActnew.User_Name__c.addError('Duplicate record on this User Name');
}
}
}
}
To check multiple condition to avoid duplication just go through the below link,
Here I have checked firstname, lastname and Email fields to avoid duplicate records
http://salesforcekings.blogspot.in/2013/11/salesforce-deduplication-trigger-on.html
If you found this answer helpful to you... Please accept this as a Solution and give kudos by clicking on the star icon.
Thanks and Regards,
Arunkumar.R | Salesforce Certified Developer
If you found this answer helpful to you... Please accept this as a Solution and give kudos by clicking on the star icon.
It works for me
please help me... to resolve my problem
Avoid Duplicate Name Only on master in Master-Detail relationship
Hi,
I have two custom object Claim__c(master) and Labour_Cost__c(detail).
They have master detail relationship.
I want to create a Trigger which prevent duplicate Name(standard field which is created default when we create any custom object) only on master object.
but we can multipe record with same Name from detail object and we select it from lookup field (Claim_Name__c) on detail object.
plz write a trigger for same.
for example: I have a record on Master Object with Name='S1 Claim' .
if i create a new record with Name 'S1 Claim' then it will display an error 'record already exists with same name'.
But,I can create multiple record with Name 'S1 Claim' from detail object.
Thanks,
Ghulam
Lets say prevent duplicate account creation :-
Create a text field :- "say X".
Select checkboox unique.*********
No need to expose in any page layout.
Create a workflow rule :- Name it as "say Y "
Created and every time it is edited
---Criteria :Account name is not equal to null .SAVE
Action:Field update :--name the field update as "say A"
From drop down choose text field "X".
In formula editior : Name(NOTE:-as account name api is name ).
SAVE .ACTIVATE .
Now it is ready ..
Create account >> Go to account tab in header>>NEW>> in account name put "TEST1"
Create another account with same account name"TEST1".
Hope above method helps you..
*********NOTE :- but many of you are thing what if i create 2nd account with name 'test1'--DO it prevent that time also.
So answer is it is as per business requirement :
If business wants : name should unique subjective to case insensitive means if TEST1 and test 1 should be treated as same account than check checkbox --->Treat "ABC" and "abc" as duplicate values (case insensitive) while creating text field "X"
If business wants : name should unique subjective to case sensitive means if TEST1 and test 1 should be treated as different account than check checkbox --->Treat "ABC" and "abc" as different values (case sensitive) while creating text field "X".
Santhosh Aloor
Below post can be used to avoid Duplicate Fields by using trigger :
Here i have used Email for my requirement .Please use Name field in the place of Email
Please find the below post further information:
Avoid Duplicate for Insert Operation By Using Apex Trigger (https://salessforcehacks.blogspot.com/2019/12/avoid-duplicate-fields-using-apex.html)
And the below Post explaines how to avoid Duplicate Fields by updating the above code :
Avoid Duplicates for both insert and Update Operation (https://salessforcehacks.blogspot.com/2019/12/avoid-duplicate-fields-using-apex_21.html)
I have created insert and update operation seperately to make you understand Clearly.
trigger AccountDuplicateCheckTrigger on Account (before insert) {
//Preparing Account names in Set from trigger.new
Set<String> nameSet = new Set<String>();
for(Account acc : trigger.new){
nameSet.add(acc.name);
}
// getting the list of accounts in database with the account name we entered ( trigger.new)
List<Account> accList = new List<Account>([select id,name from Account where name in: nameSet]);
for(Account a : trigger.new){
if(accList.size() > 0 )
a.addError('Account already exists in your Organization with name '+a.name);
}
}
WAY2
--
Check Using null condition
trigger DemoTrigger on Account(before insert,before update) {
List<Account> accList=new List<Account>([select id,name from Account]);
map<String,Account> accmap=new map<String,Account>();
for(Account acc:accList){
accmap.put(acc.Name,acc);
}
for(Account acc:Trigger.new){
if(accmap.get(acc.Name)!=null){
acc.adderror('Name already exists');
}
}
}
WAY3
----
Check using containsKey condition
trigger DemoTrigger on Account(before insert,before update) {
List<Account> accList=new List<Account>([select id,name from Account]);
map<String,Account> duplicateMap=new map<String,Account>();
//get old records
for(Account acc:accList){
duplicateMap.put(acc.Name,acc);
}
//compare old records with records in trigger.new
for(Account acc:Trigger.new){
if(duplicateMap.containsKey(acc.Name)){
acc.adderror('Name already exists');
}
}
}
http://www.sfdcamplified.com
https://www.sfdcamplified.com/2020/11/trigger-prevent-duplicate-records-based-on-multiple-fields.html (http://Check Below Links: https://www.sfdcamplified.com/2020/11/trigger-prevent-duplicate-records-based-on-multiple-fields.html https://www.sfdcamplified.com/2020/11/avoid-creating-duplicate-trigger.html)
https://www.sfdcamplified.com/2020/11/avoid-creating-duplicate-trigger.html