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
sampath pallisampath palli 

Creating a Trigger on Account object

Hi 

I have an requirement when i am trying to insert account with same name which is alredy exists then it prevents the creation of duplicate record
I have written a code but i am getting error "Initial term of field expression must be a concrete SObject: List<Account>"

Please chech my code

trigger Tsen2 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){
            acc.Name.addError('you cannot create duplicate values');
        }
    }

}

Thanks in Advance
Best Answer chosen by sampath palli
Amit Chaudhary 8Amit Chaudhary 8
sample code for you without SOQL inside for loop
trigger AccountInsert on Account (before insert,before update) 
{
	Set<Id> setAccID = new Set<ID>();
	Set<Id> setAccName = new Set<ID>();

	for(account acc: trigger.new )
    {
		setAccName.add(acc.Name);
		setAccID.add(acc.id);
	}
	
	List<Account> lstAccount = [ select id, name from account where name=:setAccName and id not in :setAccID ] ;

	Map<String,Account> MapAccount = new Map<String,Account>();
	For(Account acc : lstAccount )
	{
		MapAccount.put(acc.name,acc);
	}
	
	for(account acc: trigger.new )
    {
        if( MapAccount.containsKey(acc.name) )
        {
            acc.name.adderror('account name already existed');
        }
    }

}

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check in your. You have any class with Account Name if yes then please rename or delete

Please try to update your code like below:-
trigger Tsen2 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 and id != a.id ];
        if(acc.size()>0)
		{
            a.Name.addError('you cannot create duplicate values');
        }
    }
}
NOTE:- Adding SOQL inside the for loop is not a good pratice

Please let us know if this will help you

Thanks
AMit Chaudhary
Nayana KNayana K
trigger Tsen2 on Account (before insert, before update)
{
    AccountTriggerHandler objAccountHandler = new AccountTriggerHandler();
    if(Trigger.isBefore && Trigger.isInsert)
        objAccountHandler.onBeforeInsert(Trigger.New);
    else if(Trigger.isBefore && Trigger.isUpdate)
        objAccountHandler.onBeforeUpdate(Trigger.oldMap, Trigger.NewMap);

}


public with sharing class AccountTriggerHandler
{
    public AccountTriggerHandler()
    {
    }

    public void onBeforeInsert(List<Account> lstNewAccount)
    {
        
        preventDuplicates(new Map<Id,Account>(), lstNewAccount);
        
    }
    
    public void onBeforeUpdate(Map<Id,Account> mapOldAccount, Map<Id,Account> mapNewAccount)
    {
        
        preventDuplicates(mapOldAccount, mapNewAccount.values());
        
    }
    
    private void preventDuplicates(Map<Id,Account> mapOldAccount, List<Account> lstNewAccount)
    {
        Map<String,Account> mapStrNameRatingToAccount = new Map<String,Account>();
        Set<String> setRating = new Set<String>();
        Set<String> setName = new Set<String>();
        
        // Collect All Names from Trigger.New
        for(Account objAccount : lstNewAccount)
        {
            /* 1. On insert
            OR
            2. On update but only if Name or Rating is changed
            
            */
            if(Trigger.isInsert ||
                (Trigger.isUpdate &&
                    (mapOldAccount.get(objAccount.Id).Name != objAccount.Name
                        ||
                    mapOldAccount.get(objAccount.Id).Rating != objAccount.Rating
                    )
                )
            )
            {
                setRating.add(objAccount.Rating);
                setName.add(objAccount.Name);
                mapStrNameRatingToAccount.put(objAccount.Name + '__' + objAccount.Rating, objAccount);
            }
            
        }
            
        
        // iterate over matching Account and add error
        for(Account objMatchingAccount : [SELECT Id,Name
                                         FROM Account
                                         WHERE Name IN:setName AND Rating IN:setRating])
        {
            String strNameRatingPair = objMatchingAccount.name + '__' + objMatchingAccount.Rating;
            if(mapStrNameRatingToAccount.containsKey(strNameRatingPair))
                mapStrNameRatingToAccount.get(strNameRatingPair).addError('You cannot create duplicate values!');
        }
    }
}
Amit Chaudhary 8Amit Chaudhary 8
sample code for you without SOQL inside for loop
trigger AccountInsert on Account (before insert,before update) 
{
	Set<Id> setAccID = new Set<ID>();
	Set<Id> setAccName = new Set<ID>();

	for(account acc: trigger.new )
    {
		setAccName.add(acc.Name);
		setAccID.add(acc.id);
	}
	
	List<Account> lstAccount = [ select id, name from account where name=:setAccName and id not in :setAccID ] ;

	Map<String,Account> MapAccount = new Map<String,Account>();
	For(Account acc : lstAccount )
	{
		MapAccount.put(acc.name,acc);
	}
	
	for(account acc: trigger.new )
    {
        if( MapAccount.containsKey(acc.name) )
        {
            acc.name.adderror('account name already existed');
        }
    }

}

 
This was selected as the best answer
sampath pallisampath palli
Thank you all
sales@myvarmasales@myvarma
hey when new lead converts to account if the new account name already their the data in new account must update in old account 
any one help me