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
SCTSCT 

Updating an Account from within trigger

I am trying to merge accounts that are being upserted from a dataload. However, when the account is matched, and I subsequently try to update the parent record, the parent is not actually being updated in salesforce. No unexpected errors are occuring.

(The Trigger is in fact also being triggered for the record in the mergedAccounts list...yet it does not appear to save). What am I missing?
//Parents are any accounts that exist in Salesforce that were not imported from UFOS 
        List<Account> parents = [SELECT Id,Name,Sales_Rep__c,Active__c,BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet,Comments__c,Email__c,
                                 Fax,InternalNotes__c,Phone,RecordTypeId,Sales_Rep_Initials__c,ShippingCity,ShippingCountry,ShippingPostalCode,ShippingState,ShippingStreet,
                                 Sols_Planner__c,Toll_Free__c,UFOS_Account_Type__c,UFOS_Contact1_Ext__c,UFOS_Contact1_Phone__c,UFOS_Contact2_Ext__c,UFOS_Contact2_Phone__c,
                                 UFOS_ID__c, Type FROM Account WHERE UFOS_ID__c = ''];
        
 
        List<Account> mergedAccounts = new List<Account>();
        
        for (Account a : Trigger.new)
        {
            //see if this account has a match in the parents list.
            for(Account p : parents)
            {
                if(p.Name.getLevenshteinDistance(a.Name) <= 2)
                {

                    System.Debug('Parent---->' + p.Name + ' | ' + p.Id + ' | ' + p.Type);                
                    System.Debug('Account--->' + a.Name + ' | ' + a.Id + ' | ' + a.Type);
                    
                    if(a.Type == 'UFOS Customer')
                    {
                        //Join this account to the parent....
                        p.Sales_Rep__c = a.Sales_Rep__c;
                        p.Active__c = a.Active__c;
                        p.BillingCity = a.BillingCity;
                        p.BillingCountry = a.BillingCountry;
                        p.BillingPostalCode = a.BillingPostalCode;
                        p.BillingState = a.BillingState;
                        p.BillingStreet = a.BillingStreet;
                        p.Comments__c = a.Comments__c;
                        p.Email__c = a.Email__c;
                        p.Fax = a.Fax;
                        p.InternalNotes__c = a.InternalNotes__c;
                        p.Name = a.Name;
                        p.Phone = a.Phone;
                        p.RecordTypeId = a.RecordTypeId;
                        p.Sales_Rep_Initials__c = a.Sales_Rep_Initials__c;
                        p.ShippingCity = a.ShippingCity;
                        p.ShippingCountry = a.ShippingCountry;
                        p.ShippingPostalCode = a.ShippingPostalCode;
                        p.ShippingState = a.ShippingState;
                        p.ShippingStreet = a.ShippingStreet;                    
                        p.Sols_Planner__c = a.Sols_Planner__c;
                        p.Solution_Planner__c = a.Solution_Planner__c;
                        p.Toll_Free__c = a.Toll_Free__c;
                        p.UFOS_Account_Type__c = a.UFOS_Account_Type__c;
                        p.UFOS_Contact1_Ext__c = a.UFOS_Contact1_Ext__c;
                        p.UFOS_Contact1_Phone__c = a.UFOS_Contact1_Phone__c;
                        p.UFOS_Contact2_Ext__c = a.UFOS_Contact2_Ext__c;
                        p.UFOS_Contact2_Phone__c = a.UFOS_Contact2_Phone__c;

                        p.UFOS_ID__c = a.UFOS_ID__c;                         
                        
                        mergedAccounts.add(p);
                        System.debug('mergedAccounts.size = ' + mergedAccounts.size());
                        
                        a.addError('Customer merged with Salesforce account: ' + p.Id);
                        
                        
                        
                    }
                    else //match all Accounts that are not a UFOs Customer to
                    {
                        //Do not want to create a circular dependency
                        if(a.Id != p.Id)
                        {
                            a.ParentId = p.Id;
                        }
                        
                    }
                }
            }
            
        }
        
        System.debug('final mergedAccounts.size = ' + mergedAccounts.size());
        
        if(!mergedAccounts.isEmpty())
        {
            System.debug('Merging accounts: ' + mergedAccounts);
            update mergedAccounts;
        }

 
Forza di SognoForza di Sogno
Can you add more system debug statements within all if/elses to confirm those areas are actually being hit by your code?
Also, please post the entire trigger.
SCTSCT
trigger AccountTrigger on Account (before insert, before update) 
    {
        //Parents are any accounts that exist in Salesforce that were not imported from UFOS (at this point)
        List<Account> parents = [SELECT Id,Name,Sales_Rep__c,Active__c,BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet,Comments__c,Email__c,
                                 Fax,InternalNotes__c,Phone,RecordTypeId,Sales_Rep_Initials__c,ShippingCity,ShippingCountry,ShippingPostalCode,ShippingState,ShippingStreet,
                                 Sols_Planner__c,Toll_Free__c,UFOS_Account_Type__c,UFOS_Contact1_Ext__c,UFOS_Contact1_Phone__c,UFOS_Contact2_Ext__c,UFOS_Contact2_Phone__c,
                                 UFOS_ID__c, Type FROM Account WHERE UFOS_ID__c = ''];
        
        List<Account> mergedAccounts = new List<Account>();
        
        for (Account a : Trigger.new)
        {
            System.debug('The Account: ' + a);
            //see if this account has a match in the parents list.
            for(Account p : parents)
            {
                if(p.Name.getLevenshteinDistance(a.Name) <= 2)
                {

                    System.Debug('Parent---->' + p.Name + ' | ' + p.Id + ' | ' + p.Type);                
                    System.Debug('Account--->' + a.Name + ' | ' + a.Id + ' | ' + a.Type);
                    
                    if(a.Type == 'UFOS Customer')
                    {
                        //Join this account to the parent....
                        p.Sales_Rep__c = a.Sales_Rep__c;
                        p.Active__c = a.Active__c;
                        //p.BillingAddress = a.BillingAddress;
                        p.BillingCity = a.BillingCity;
                        p.BillingCountry = a.BillingCountry;
                        p.BillingPostalCode = a.BillingPostalCode;
                        p.BillingState = a.BillingState;
                        p.BillingStreet = a.BillingStreet;
                        p.Comments__c = a.Comments__c;
                        p.Email__c = a.Email__c;
                        p.Fax = a.Fax;
                        p.InternalNotes__c = a.InternalNotes__c;
                        p.Name = a.Name;
                        p.Phone = a.Phone;
                        p.RecordTypeId = a.RecordTypeId;
                        p.Sales_Rep_Initials__c = a.Sales_Rep_Initials__c;
                        //p.ShippingAddress = a.ShippingAddress;
                        p.ShippingCity = a.ShippingCity;
                        p.ShippingCountry = a.ShippingCountry;
                        p.ShippingPostalCode = a.ShippingPostalCode;
                        p.ShippingState = a.ShippingState;
                        p.ShippingStreet = a.ShippingStreet;                    
                        p.Sols_Planner__c = a.Sols_Planner__c;
                        p.Solution_Planner__c = a.Solution_Planner__c;
                        p.Toll_Free__c = a.Toll_Free__c;
                        p.UFOS_Account_Type__c = a.UFOS_Account_Type__c;
                        p.UFOS_Contact1_Ext__c = a.UFOS_Contact1_Ext__c;
                        p.UFOS_Contact1_Phone__c = a.UFOS_Contact1_Phone__c;
                        p.UFOS_Contact2_Ext__c = a.UFOS_Contact2_Ext__c;
                        p.UFOS_Contact2_Phone__c = a.UFOS_Contact2_Phone__c;
                        p.UFOS_ID__c = a.UFOS_ID__c;                         
                        
                        mergedAccounts.add(p);
                        System.debug('mergedAccounts.size = ' + mergedAccounts.size());
                        
                        a.addError('Customer merged with Salesforce account: ' + p.Id);
                    }
                    else //match all Accounts that are not a UFOs Customer to
                    {
                        System.debug('In else...');
                        //Do not want to create a circular dependency
                        if(a.Id != p.Id)
                        {
                            System.debug('update parentId');
                            a.ParentId = p.Id;
                        }
                        
                    }
                }
            }
            
        }
        
        System.debug('final mergedAccounts.size = ' + mergedAccounts.size());
        
        if(!mergedAccounts.isEmpty())
        {
            System.debug('Merging accounts: ' + mergedAccounts);
            upsert mergedAccounts;
        }
        
    }

 
Forza di SognoForza di Sogno
I think it would help if you added an else statement, with debug statement, for every if statement? This will tell you if the correct records are being returned or found.