Trigger CheckForDuplicate on Product_Segment__c (before insert)
{
Set<String> psName = new Set<String>();
Set<String> psCode = new Set<String>();
Set<String> categoryName = new Set<String>();
List<Product_Segment__c> existingPS = new List<Product_Segment__c>();
for(Product_Segment__c tempPS : Trigger.new)
{
psName.add(tempPS.Name);
psCode.add(tempPS.Product_Segment_Code__c);
categoryName.add(tempPS.Product_Category_Name__c);
}
existingPS = [SELECT Id,
Name,
Product_Segment_Code__c,
Product_Category_Name__c
FROM Product_Segment__c
WHERE Name=: psName
AND Product_Segment_Code__c=: psCode
AND Product_Category_Name__c=: categoryName];
for(Product_Segment__c tempPS : Trigger.new)
{
for(Product_Segment__c existPS : existingPS)
{
if(tempPS.Name == existPS.Name
&& tempPS.Product_Segment_Code__c == existPS.Product_Segment_Code__c
&& tempPS.Product_Category_Name__c == existPS.Product_Category_Name__c)
{
tempPS.addError('Duplicate Record!!!!!');
}
}
}
}
No comments:
Post a Comment