3

I am trying to match existing accounts based on a custom field but not having any luck with my code.

If in an upcoming upsert there is no Salesforce ID, then I want to check for existing accounts with the custom UniqueID field. If UniqueID matches, then I want that account to be updated.. If they don't match, then I want a new account created.

I'm getting the following error: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.matchUniqueID: line 8, column 1

trigger matchUniqueID on Account(Before Insert, Before Update) 
{
    Map<ID, Account> existingAccountMap = new Map<ID, Account> ([Select Id, uniqueID__c, ClassCode__c FROM Account  WHERE UniqueID__c != Null AND ClassCode__c = Null]);
    List<Account>exAccounts = new List<Account>();

    for(account acc : trigger.new) 
    {
        if(acc.uniqueID__c == existingAccountMap.get(acc.ID).uniqueID__c &&  acc.Classcode__c == Null) 
        {
            Account a = new Account();
            a.ID = existingAccountMap.get(acc.ID).ID;
            exAccounts.add(a);
        }   

        if(exAccounts.size() > 0)
        {
            update exAccounts;
        }
    }
}
1

When a new account is inserted, your trigger is checking for the account Id, which in this case is null - the records has not been given an ID yet, it hasn't been committed to the database yet. On the other hand, you can check the Unique Id on the newly inserted account, so you could use that to determine matching accounts in the system.

Here is a better solution:

trigger matchUniqueID on Account(Before Insert, Before Update) 
{
    Map <Id, Id> uniqueIdMap = new Map <Id, Id> ();

    for (Account account : trigger.new)
    {
        uniqueIdMap.put(account.UniqueID__c, null);
    }

    for (Account account : [SELECT Id, UniqueID__c FROM Account WHERE UniqueID__c IN :uniqueIdMap.keySet()])
    {
        uniqueIdMap.put(uniqueIdMap.get(account.UniqueID__c), account.Id);
    }

    List <Account> existingAccountsToUpdate = new List <Account> ();

    for (Account account : trigger.new)
    {
        if (uniqueIdMap.containsKey(account.UniqueID__c) && account.Classcode__c == null)
        {
            // Existing account found
            Account existingAccount = new Account(Id = uniqueIdMap.get(account.UniqueID__c));
            // Set other field values to update
            // existingAccount.SOMEFIELD = SOMEVALUE;
            existingAccountsToUpdate.add(existingAccount);
        }
        else
        {
            // No existing accounts found
            // Set some other fields on the newly inserted account if you wish
            // account.SOMEFIELD = SOMEVALUE;
        }
    }

    update existingAccount;
}
  • 1
    @Mindfreak14 is also trying to access acc.id , in the same line. Wouldn't it be null for before insert? – Prajith Feb 26 '16 at 4:33
  • Good point :) Typed on the fly without much thinking. Updated the answer. +1 – Boris Bachovski Feb 26 '16 at 4:46
  • Thanks Boris - confused about this part: for (Account account : trigger.new) { uniqueIdMap.add(account.UUID__c); } Getting a method does not exist error : [Map].add(String) – mindfreak14 Feb 26 '16 at 4:55
  • @mindfreak14 You can't use 'add' on a map, it can only be used for set or list. I think you are aiming to use [Map]. put(key, value) – Prajith Feb 26 '16 at 5:04
  • // Existing account found Account existingAccount = new Account(Id = uniqueIdMap.get(account.UniqueID__c)); For that.. i should be getting the actual ID itself correct? uniqueIDMap.get(account.ID).ID) ? right now it's giving me an error saying invalid ID since it's trying to match UUID with ID – mindfreak14 Feb 26 '16 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.