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
SfdcDevLJSfdcDevLJ 

Match UniqueID with Existing Account before Insert

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);
        }
       
    }

    update existingAccountsToUpdate;
}

I am doing data upserts using jitterbit and we use SalesforceID as our external ID. However, some records coming in dont have a ID yet so in that case i want to match those records based on another field called UniqueID__c. I have the trigger pasted above.. but it's pulling the UniqueID__c and tries to match with SalesforceID. How can i fix this. 

 Execution of BeforeUpdate caused by: System.StringException: Invalid id: 1234355: External entry point

sfdcsushilsfdcsushil
Your map needs to be like this - Map<String, Id> uniqueIdMap = new Map<String,Id>()
sfdcsushilsfdcsushil
Id type only takes salesforce ids, thats why
SfdcDevLJSfdcDevLJ

Thanks - fixed that issue : But now i'm getting this error on Update: 

execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.matchUniqueID: line 30, column 1
sfdcsushilsfdcsushil
In first loop you are putting nulls as value. If id is null for any unique id, you will get this error. You can handle that by adding null check in last loop/
But are you not doing upsert? For upsert, you just need to give external id (Uniqueu id) and it will automatically do insert if record is not matched based on unique id and update if record was matched. I am not sure why you are writing this trigger. 
SfdcDevLJSfdcDevLJ
I am doing upsert using SalesforceID since most of the records coming in will have the SalesforceID .. But there maybe few records that won't have SalesforceID but will already be in Salesforce... that why i hae to match those with the UniqueID
sfdcsushilsfdcsushil
ok. Let me know if above helps. 
SfdcDevLJSfdcDevLJ

Now i don't get an error but doing an upsert just creates a new record even though uniqueID already exists on another account.

trigger matchUniqueID on Account(Before Insert, Before Update) 
{
    Map <string, Id> uniqueIdMap = new Map <string, 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 && account.ID!=Null)
        {
          
            Account existingAccount = new Account(Id = uniqueIdMap.get(account.ID));
         
            existingAccountsToUpdate.add(existingAccount);
        }
       
    }

    update existingAccountsToUpdate;
}
Mahesh DMahesh D
Hi,

Few points your trigger:

--> We shouldn't do any DML operations in the before trigger.
--> We have to be very careful when we are doing the DML operation on the same object records, because it may lead to recursive trigger calling.

Please provide more information about your actual requirement, so that can able to give appropriate answer.

Regards,
Mahesh
 
Temoc MunozTemoc Munoz
Is "UniqueId_c" set as unique in Salesforce? If so, have you tried Database.upsert? You can then check your database results, and check for duplicates. Or you can also try : upsert existingAccountsToUpdate UniqueID__c;

https://developer.salesforce.com/forums/?id=906F00000008rVPIAY
SfdcDevLJSfdcDevLJ
 We'll be doing a large migration of records from legacy system into Salesforce and then send the the Salesforce ID back to the legacy system and that's our externalID. So those upserts will work fine.. However, when new records come from the legacy system later on - they won't automatically go back with the salesforce ID until someone makes a change on salesforce end.
That's where the issue comes in : If a change is made to the record in the legacy system before the record is sent back to it with the Salesforce ID ..The upsert will just create a new record.
Unless there is a better way to send new records coming to Salesforce directly back to the Legacy system without any modifications? Thanks
venkat-Dvenkat-D
Dont you have unique Ids in legacy system? if not create a new field in legacy system. use that value as External Id and always do upsert with that value. I do not think what you are asking can be done.