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
hamshuhamshu 

Incompatible key type String for MAP<Integer,Account> at line 9 column 16

HI,

I am trying to perform duplicate check for phone number field in account but i am getting following error..

 

trigger Duplicatecheck on Account (before insert,before update)
{
    
    map<integer,Account> getphone=new map<integer,Account>();
    for(Account acc:trigger.new)
    {
        if(acc.Phone!=null &&(trigger.isinsert || acc.Phone!=trigger.oldmap.get(acc.id).Phone))
        {
            if(getphone.containskey(acc.Phone))//error:Incompatible key type String for MAP<Integer,Account>
            {
                acc.Phone.adderror('A New Lead As Same Email');
            }
        }
        else
        {
            getphone.get(acc.Phone);
        }
        
     }
    
    for(account accc:[select Phone from account where Phone in:getphone.keyset()])
    {
        account newaccount=getphone.get(accc.Phone);
        newaccount.Phone.adderror('Lead Exits With Same email');
    }
    
}

Dhaval PanchalDhaval Panchal

You map is MAP<Integer,Account> thats means your key must be of integer type. Where account.phone is not a numeric/integer field. Its a string type field. So you cannot use this field to check map.

If you want to make account.phone as a key then you have to create map like Map<String, Account>.