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
sfdc_beginner7sfdc_beginner7 

Hi All,I am new to coding .I have written a trigger to prevent duplicate account ,my trigger is :

trigger prevent_duplicate_account on Account (before insert) {
    map<id,Account> mapAcc = new map<id,Account>([Select id,name,rating from Account ]);
    for(Account a : Trigger.new)
    {
        if(a.Name==mapAcc.get(a.id).name && a.rating==mapAcc.get(a.id).rating)
            a.adderror('you cant add duplicate account');
    }

}
I have already an account with name as IGA ,i am updating this account with rating =warm ,it is preventing to save record .since I have used && condition ,if both are true then only it should save the record to save . Also If i am creating a record with name as IGA and rating different from the previous one than also I am getting error .What can be done please help
Thanks in advance!!
Vinod AdminVinod Admin
Hi Sonal,

You need to use before Update event trigger as well as. 

Trigger preven_Duplicat_account on Account(before insert, before update)

As per your above trigger when ever the both scenarios are true it will throughs the error. Insted of above code you can use below peace of code.

trigger AccountDuplicateTrigger on Account (before insert,before update) { for(Account a:Trigger.new) { List<Account> acc=[select ID from account where Name=:a.Name and Rating=:a.rating]; if(acc.size()>0) { a.adderror('You cannot create a dulplicate account'); } } }

 
Amit Chaudhary 8Amit Chaudhary 8
Update your code like below
trigger prevent_duplicate_account on Account (before insert , before update) 
{
	Set<String> setId = new Set<String>();
	Set<String> setName = new Set<String>();
	Set<String> setRating = new Set<String>();
	
    for(Account a : Trigger.new)
    {
        if(a.Name != null && a.rating != null)
		{
			setId.add(a.id);
			setName.add(a.Name);
			setRating.add(a.rating);
		}
	}
		
    List<Account> lstAccount = [Select id,name,rating from Account where name in :setName and rating in:setRating and id not in :setId ];
	Map<String , Account> mapAccount = new Map<String , Account>();
	
	For(Account acc : lstAccount){
		String key = acc.name +'-'+acc.rating;
		mapAccount.put(key,acc);
		
	}
	
    for(Account a : Trigger.new)
    {
        if(a.Name != null && a.rating != null)
		{
			String key = a.name +'-'+a.rating;
			if( mapAccount.containsKey(key) )
			{
				a.adderror('you cant add duplicate account');
			}	
		}	
    }
}

Let us know if this will help you
 
sfdc_beginner7sfdc_beginner7
Amit Chaudhary ,I have used your code ,but the problem i am facing is like my account name is IGA and rating is warm  ,I am updating rating to cold it is giving error as duplicate .It sould not be duplicate because we have no record with name as IGA and rating as cold.
sfdc_beginner7sfdc_beginner7
Thanks its working I have added one condition
if(listacc !=null){
    for(Account a1 : listacc)
    {
        string key = a1.name +'-'+ a1.rating;
        mapacc.put(key,a1);
    }}