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 

Update field in existing Account from trigger.new Avoid Duplicate

trigger AccountDuplicateTrigger on Account (before insert, before update) {
    //Create map of existing person accounts that don't have a externalID
	map<Id, Account> existingAccountMap = new Map<ID, Account>([Select ID, Name, externalID__c
                                                                FROM Account 
                                                                WHERE isPersonAccount=True AND externalID__c = Null]);
    //For new trigger accounts check if Name exists in existingAccountmap
    for(Account a : trigger.new) {
        map<ID, Account>newAccountMap = new Map<ID, Account>([SELECT ID, Name, externalID__c FROM Account
                                                                 WHERE ID IN :trigger.newMap.keySet()]);
    //If new account name matches an existing account then put UUID in the existing account
        if(a.name = existingAccountMap.get(a.ID).Name)
        {
          .........
        }
    
    }
}

We have an integration with a legacy system and we use an Unique ExternalID to send personAccount upserts back and forth between salesforce and the legacy system using JItterbit.
However, that externalID is provided by the legacy system, When a new record is created in Salesforce it is sent to the legacy system where it gets the Unique ExternalID. Problem is on the way back to Salesforce since the ExternalID isn't already on the Account, Jitterbit creates a new duplicate account. I was workign on the code above but not sure if it will get me what i'm trying to accomplish.
Shikha AgashiShikha Agashi
I would suggest you to create unique autonumber field. whenever you sent account information back send this autonumber and when recieving legacy externalId along with unique autonumber, it would update right account as well do not create new account. While creating account you can make sure create only when autonumber=='' otherwise just update account with autonumber
Shikha AgashiShikha Agashi
In above code, you are matching name. Name is not always unique. So you need some unique identifier on object.
SfdcDevLJSfdcDevLJ
You are correct, i forgot we do have an Auto number field on every Record. How would i insert that logic in the trigger? I can't enter any logic in Jitterbit so it will still try to create a new record.
Shikha AgashiShikha Agashi
Can you please paste your entire code as well as autonumber field api name? Is this autonumber Unique?
SfdcDevLJSfdcDevLJ
trigger AccountDuplicateTrigger on Account (before insert, before update) {
    //Create map of existing person accounts that don't have a uniqueID
	map<Id, Account> existingAccountMap = new Map<ID, Account>([Select ID, SFID__c, uniqueID__c
                                                                FROM Account 
                                                                WHERE isPersonAccount=True AND uniqueID__c = Null]);
    //For new trigger accounts check if Name exists in existingAccountmap
    for(Account a : trigger.new) {
        map<string, Account>newAccountMap = new Map<string, Account>([SELECT SFID__c, uniqueID__c FROM Account
                                                                 WHERE SFID__c IN :trigger.newMap.keySet()]);
    //If SFID matches an existing account then put uniqueID in the existing account
        if(a.SFID__c == existingAccountMap.get(a.ID).SFID__c)
        {
     // Rest of logic here
        }
    
    }
}
Yes the autonumber is unique
 
Shikha AgashiShikha Agashi
Please entire code. You are even performing some DML operation, I need to look into it
SfdcDevLJSfdcDevLJ
I didn't get to the DML operation part yet, that's where i am stuck. Whether if i need to make a list of the account IDs once i have the SFIDs and then perform update. Not sure.
Shikha AgashiShikha Agashi
I releaized it is creating new account irrespective of update. Are you sending account Id and recieving back? Use below code in combination of SFIDs and accountIds
 
trigger AccountDuplicateTrigger on Account (before insert, before update) {
    
	
	if(trigger.isInsert){
	                 // if it is before insert then there will be no account Id only. 
	}
	else
	if(trigger.isUpdate){
		 for (Account newAcct : trigger.new) {
			Account oldAcct = trigger.oldmap.get(newAcct.Id);	

                                              //with OldAcct you can built your logic
		 }
	}
	
	
	
	
}

 
SfdcDevLJSfdcDevLJ
I am not sending account ID to the legacy system.. Only thing going to legacy system after new account is created in Salesforce is the Autonumber field(SFID__c) and some other fields but the legacy system sends the record back with a uniqueID field. 
I will need to try to match the incoming accounts with the SFID__c if UniqueID field is blank
Deepak GulianDeepak Gulian
trigger AccountDuplicateTrigger on Account (before insert, before update) {

	map<Id, Account> existingAccountMap = new Map<ID, Account>([Select ID, SFID__c, uniqueID__c FROM Account WHERE isPersonAccount=True AND uniqueID__c = Null]);
List<Account> exAccounts = new List<Account>();
   
   //For new trigger accounts check if Name exists in existingAccountmap
    for(Account a : trigger.new) {
    
    //If SFID matches an existing account then put uniqueID in the existing account
        if(a.SFID__c == existingAccountMap.get(a.ID).SFID__c && a.uniqueID__c != null)
        {
              Account acc = new Account(
                  Id = existingAccountMap.get(a.ID).ID),
                  uniqueID__c = a.uniqueID__c
              );
              exAccounts.add(acc);
        }
    
    }
    if(exAccounts.size() > 0){
          update exAccounts;
    }
}