+ Start a Discussion
santhosh konathala 17santhosh konathala 17 

can anybody tell me the reason .Below is my Trigger code have compiled well but the Trigger functionality is working in update case only?

Trigger AccountAvoidDuplicate on Account(before insert,before update)
{
Map<id,account> map1=new map<id,account>();
list<account> acclist=[select id,name from account];
list<account> acclist1=new list<account>();
for(account acc:Trigger.new)
{
for(account a:acclist)
{
acclist1.add(a);
map1.putall(acclist1);


if(map1.containskey(acc.id))
{
acc.adderror('plz insert unique values');
}
}
}
}
 
Best Answer chosen by santhosh konathala 17
sfdcMonkey.comsfdcMonkey.com
hi santosh
your trigger is on before insert so in this condition you don't have account id so how you can fill map with account id
cahnge you code to avoid duplicate value
try this trigger
trigger AccountAvoidDuplicate on account(before insert,before update)
{
 
         Set<string> name= new Set<string>();

         for(account acc : Trigger.new)
         {
           name.add(acc.name);
         }

       List<account> duplicateaccountList = [Select name From account where name = :name];

       Set<string > duplicateaccIds= new Set<string >();

       for(account dup: duplicateaccountList )
       {
         duplicateaccIds.add(dup.name);
       }

       for(account a : Trigger.new)
       {
            if(a.name!=null)
            {
               if(duplicateaccIds.contains(a.name))
               {
                 a.addError('plz insert unique values');
               }
            
            }
       }
}
now if user create / update account with existing account name .user get a error message
Thanks
mark it best answer if it helps you :)
 

All Answers

sfdcMonkey.comsfdcMonkey.com
hi santosh
your trigger is on before insert so in this condition you don't have account id so how you can fill map with account id
cahnge you code to avoid duplicate value
try this trigger
trigger AccountAvoidDuplicate on account(before insert,before update)
{
 
         Set<string> name= new Set<string>();

         for(account acc : Trigger.new)
         {
           name.add(acc.name);
         }

       List<account> duplicateaccountList = [Select name From account where name = :name];

       Set<string > duplicateaccIds= new Set<string >();

       for(account dup: duplicateaccountList )
       {
         duplicateaccIds.add(dup.name);
       }

       for(account a : Trigger.new)
       {
            if(a.name!=null)
            {
               if(duplicateaccIds.contains(a.name))
               {
                 a.addError('plz insert unique values');
               }
            
            }
       }
}
now if user create / update account with existing account name .user get a error message
Thanks
mark it best answer if it helps you :)
 
This was selected as the best answer
santhosh konathala 17santhosh konathala 17
Thanks Soni,
 I am learning So much from your side.A great thanks to you.