function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mdangondmdangond 

Account Insert Apex Trigger

I need to write an Apex Trigger that will check if an account exists when a user tries to create a new account, and either allow the insert if the account is not found or block the insert and display a message telling the user this account cannot be created since it already exists.

 

I have never written an Apex Code and I will be going to a class in January to learn how to write Apex Code, but in the meantime (since I need this trigger ASAP) can someone provide the code to do this? I imagine this is not very complicated and it is a trigger that many other people may have already written.

 

Thanks so much in advance to whoever can help me with this Apex Trigger code.

Best Answer chosen by Admin (Salesforce Developers) 
SelectedPartnerSelectedPartner

Refer to the following segement, and make some modification to the criteria in where clause to satisfy your requirement:

 

 

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!'); } }}

 

 

 

 

All Answers

SelectedPartnerSelectedPartner

Refer to the following segement, and make some modification to the criteria in where clause to satisfy your requirement:

 

 

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!'); } }}

 

 

 

 

This was selected as the best answer
BharathwajBharathwaj

Check this one too for the account name changed to any exixting accounts.

 

for (Account account : System.Trigger.new)

{ /* Make sure we don't treat an accountName that isn't changing during an update as a duplicate. */

if ((account.Name != null) && (System.Trigger.isInsert || (account.Name != System.Trigger.oldMap.get(account.Id).Name)))

{

// Make sure another new account isn't also a duplicate

if (accountMap.containsKey(account.Name))

{

account.Name.addError('Another account has the same name.');

}

else

{

accountMap.put(account.Name, account);

}

}

}

mdangondmdangond
Thank you so much! This should do the trick.
mdangondmdangond
This one looks good too! Thanks a million :-)
monodangondmonodangond

Thanks so much SelectedPartner. This solution worked!

 

But now I need to improve on this logic just a little bit and I need to validate that the account name is following our naming standard. Our Account naming standard is "Company Name - Site City", where Site City is a custom field that has the name of the city where the company is located. So what I am now trying to do is to add one or two more lines of code that will do the following:

 

1. Check that the company name has a " - " in its name.

2. Check that the Site City value (which is required) is also part of the name.

 

Example:

 

IBM - San Francisco is the correct way to name the IBM account in San Francisco. So anything like just "IBM" would be invalid. "IBM - "would also be invalid, since it's missing the city name at the end of the name. "IBM - Boston", with a Site City = San Francisco would also be invalid since Boston is not equal to the value of Site City, in this case, San Francisco.

 

In summary:

 

Is there a way to check for the existance of " - " and a matching value of Site City on the new account name a user is trying to create? If this is true, can you please give me an example of how this code would look like? I'm already using your code.

 

Thanks a million!