You need to sign in to do that
Don't have an account?
SfdcDevLJ
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
Thanks - fixed that issue : But now i'm getting this error on Update:
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.
Now i don't get an error but doing an upsert just creates a new record even though uniqueID already exists on another account.
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
https://developer.salesforce.com/forums/?id=906F00000008rVPIAY
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