You need to sign in to do that
Don't have an account?
Trigger for Duplicate check based on City, and Street.
Hi there,
I need to enhance this trigger so it can check duplicates based on the City name, Street Name and Account name.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
trigger AccountCreateTrigger on Account (before insert) {
for (Account acc: Trigger.new)
{Account [] accs = [SELECT Name FROM Account WHERE Name = :acc.Name LIMIT 1];
if (accs.size() > 0 ) //if account already exists, block and show the information on the page
{acc.addError('Account \'' + accs[0].Name + '\' already exists. Please use existing account or contact your Salesforce Administrator for assistance.'); }
}
}
////////////////////////////////////////////
The above trigger only displays error message if the Account Name entered by User is the same to the Account Name already existed.
For example:-
a) Accucom - Baulkham Hills - is already existing.
b) Accucom Ltd. - Baulkham Hills - this value should not be allowed to save, but the user is able to save this record if the Account Name is slightly different.
How can i make sure that if the Site_City Name field is same and the Street Name, along with the Account Name then do not allow to save records.
Any recommendations???
try this, note that I have bulkified your trigger, its a big no no to put queries inside a loop in Salesforce.
This code can be made more efficient by using maps, try and explore it if you could.
Hi,
yes, the above code will work for you!
However as he said, you can still optimize it. I have tried my bit of optimzation. Mainly to remove that for within a for (that can lead to too many scripts in case of a large bulk dml)
You can refer the below code too.
Let me know if that helps :)
thank you so much.
I am trying now and will inform back what really worked.
thanks.
I have few questions -
a) Why should we NOT put any queries inside the FOR Loop? What really happens? This code has been sitting in our production org for like 3 years now and it was developed by an Admin.
I need to understand what are the effects of doing this? Why are we using the Query in this FOR statement then?
b) In the first code the LIST is used ?whats the real difference between using these two operators?
c) This set of code, may also prevent SAVING the existing records if they are edited and it already exisits in the system.
thanks for your time.
a) We should not put query inside a for loop as there is a stringent limit on salesforce of 20 queries per trigger execution, so if you do a bulk upload of account, say 100 records, your original trigger would have failed.
the query in this for loop
is a more efficient way of doing this
usually, when there is no updates to be performed in the queried data, you can directly put the query in the for statement
b) We use list mostly when records need to be iterated and updated, otherwise we can use set's as in the second code. Using set's or map's makes the code more efficient and reduces the line of code executed.
c) Yes, if the existing records need to be saved, you could either use list and then update the list, or use map's to fetch the records that need to be updated and then putting them in a list to later update. Using map's would save looping and hence recduce the lines of code executed.
Hope that's clarifies your doubts.