the perosn creates the lead with same number, and that lead already present in salesforce , shouldn't create the lead , its should throw an error creating an duplicate lead.
also i want to avoid the same number entering multiple times in salesforce , only unique number should be there , i want to write dupliactes rule for this plz help
Number__c is Number Field In lead object in My Org You Can Replace The Field Want To Restrict From Duplication
Trigger preventDuplication on Lead(before insert, before update) {
Set<string> numSet = new Set <string>() ;
If (trigger.IsBefore) {
If(trigger. Is Insert || trigger.IsUpdate){
For(lead led: trigger.new){
numSet.add(led.Number__c);
}
}
}
List<Lead> lstled = [select Id, Number__c from Lead where Number__c in :numSet];
For(Lead led2 : trigger.new){
If(lstled.size()>0){
led2.Number__c.adderror('duplicate Number found');
}
}
Please Mark It As Best Answer If It Helps Thank You!
Can You Please Explain More About Requirement
the perosn creates the lead with same number, and that lead already present in salesforce , shouldn't create the lead , its should throw an error creating an duplicate lead.
also i want to avoid the same number entering multiple times in salesforce , only unique number should be there , i want to write dupliactes rule for this plz help
Try The Below Trigger Please Mark It As Best Answer If It Helps
Thank You!
instead of using the lead trigger .
Can you set up duplicate rule with matching criteria as below and see if that helps
Matching CriteriaLead: NumberOfLeadExactMatchBlank = TRUE
Related:https://www.salesforceben.com/6-salesforce-duplicate-rules-you-need-to-activate-right-now/
https://trailhead.salesforce.com/content/learn/modules/sales_admin_duplicate_management/sales_admin_duplicate_management_unit_2