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
Claudio D'AmicoClaudio D'Amico 

Fill automatically parent account field

Hi All ,
I have to fill Parent Account field on the Child Account and the only information I've on the Child Account, that I receive through sync, is a number, of the Patent ACcountm stored in a picklist custom field named VENDOR. The same number is stored on the Parent Account as ACCOUNT NUMBER. Can some one suggest a solution on this?

Thanks
Claudio
Best Answer chosen by Claudio D'Amico
Ricky Lowe 19Ricky Lowe 19
Hi Claudio,

If I understand your question properly you want to link a child Account to a parent Account using the same field value. 

One option would be to modify the “sync” process to first query Salesforce to find the parent account using the Account Number/Vendor value. But let’s assume you can’t modify this and need to do this in Salesforce. 

You could set up a Process Builder + Flow to query the parent Account record using the Account Number/Vendor and set the Account lookup field. However, this would not be the most efficient method due to Process Builder/flow not being fully bulkified. 

The most efficient method would be to write a trigger - see a rough outline of the class that should be called from a before insert trigger context
 
public void beforeInsert() {
        Set<String> vendorNumbers = new Set<String>();

        //build set of vendor numbers
        for(Account acc : Trigger.new){
            if(String.isNotBlank(acc.vendor__c)){
                vendorNumbers.add(acc.vendor__c);
            }
        }

        //query account number + build map
        Map<String, Id> accNumToId = new Map<String, Id>();
        for(Account acc : [SELECT Id, Account_Number__c FROM Account WHERE Account_Number__c IN : vendorNumbers]){
            accNumToId.put(acc.Account_Number__c, acc.Id);
        }

        //iterate round trigger new list 
        for(Account acc : Trigger.new){
            if(String.isNotBlank(acc.vendor__c)){
                if(accNumToId.containsKey(acc.vendor__c)){
                    acc.ParentId = accNumToId.get(acc.vendor__c);
                }
            }
        }
    }

Cheers,

Ricky​​​​​​​