You need to sign in to do that
Don't have an account?
anup-prakash
Is this Trigger okay to check for duplicate name
Hi.
I have written just a few triggers and was wondering if this trigger is absolutely fine or if it lacks something.. although the trigger is working when i am inserting single entry through the window.. was wondering if it would also work when using data Loader.
This should work.
One thing you needed to do was check in the list of inserts/updates that are being submitted if there are dupes in there as well. Otherwise I could batch insert two products with the same name and your trigger wouldn't catch me
All Answers
set<String> prodNewName;
map<ID,String> prodName;
for(Prod__c p: Trigger.new){
prodNewName.add(p.Name);
}
System.debug(prodNewName);
for(Prod__c p: [select Name from Prod__c where Name IN :prodNewName]){
prodName.put(p.ID, p.Name);
}
if(prodName.size() == 0 ){
for(Prod__c p: Trigger.new){
p.addError('Product By This Name already exisits');
}
}
}
I am so sorry...
I'm pretty sure your trigger will force records that aren't dupes to fail (during bulk operations) if there is a record that actually has a dupe. Since you're not remapping it.
Try something like this:
is this piece of code fine now? or should I stick to the one you provided? and whats the difference?
I would try to use what I provided ... (It also reduces script statements).
To give you an explanation as to what the difference is, the revised code you posted doesn't handle the scenario of multiple records.
Let's say you have the following being inserted:
Record 1
Record 2
You end up doing the following:
However, what happens if only ONE record in the above has a dupe, the query will still return one record... the isEmpty check will not handle your business case correctly since the way you're logic is being done is an all or nothing scenario.
This should work.
One thing you needed to do was check in the list of inserts/updates that are being submitted if there are dupes in there as well. Otherwise I could batch insert two products with the same name and your trigger wouldn't catch me
thanks So muchh.. fo letting me know the difference. I'l do a deep dive into it and try to understand the functioning. But nevertheless thanks sooo Muchh..
You specifically asked if it could be used via the data loader, with mine you can because it checks all records being submitted.
With the other solutions, you could load an entire spreadsheet of the same name, and the system wouldn't catch it.
Are you saying the trigger stops you from adding anymore Prod__c records?
Can you post the code that you used for the final trigger?
But the other one of JayNic is not allowing me.. So your trigger is absolutely fine right?? Jaynic was saying that It'll face problem through dataLoader.. is it so? I am in dilemma..
Well, here's a modified version of my trigger that will handle that one case that JayNic pointed out.
One thing to note about triggers is, Ideally you want your triggers to only ever run if you need to do something with it (in the case of updates of records you don't need to check for dupes if the name isn't changing since you're before insert already validates dupes).
I understood Your Logic.. and Could you check for the last time as to what I have Written If this is okay?
Couple of things to compare...
Here's the latest revision of mine to ignore case sensitivity within the Map.
Now for you're trigger. A couple of things:
Other then that, you're logic looks fine.