Custom JavaScript button for account merge not executing in Lightning Experience, only works in Classic

We have a custom JavaScript button on the Account object that handles complex account merging logic. It worked perfectly in Classic, but after migrating to Lightning Experience, users click the button and nothing happens - no error, no execution.

The button code starts with:

var accountId = '{!Account.Id}';
var result = sforce.connection.query("SELECT Id, Name FROM Account WHERE ParentId = '" + accountId + "'");

I know Salesforce is deprecating JavaScript buttons, but we need this functionality working now. Users can’t merge accounts with child relationships, which is critical for our data cleanup process. Is there a quick fix to make this work in Lightning, or do we need to completely rebuild it?

Here’s the complete migration path from JavaScript button to Lightning quick action:

Understanding JavaScript Button Deprecation Salesforce deprecated JavaScript buttons in Lightning because:

  • sforce.connection API doesn’t exist in Lightning context
  • Security vulnerabilities with client-side code execution
  • Poor mobile compatibility
  • No support for Lightning security model

Your button will never execute in Lightning - this is intentional, not a bug.

Solution: Lightning Quick Action with Apex

Step 1: Create Apex Controller

public class AccountMergeController {
    @AuraEnabled
    public static List<Account> getChildAccounts(Id accountId) {
        return [SELECT Id, Name, ParentId FROM Account WHERE ParentId = :accountId];
    }

    @AuraEnabled
    public static void mergeAccounts(Id masterId, List<Id> duplicateIds) {
        // Apex merge logic with proper error handling
    }
}

Step 2: Build Lightning Quick Action

  • Go to Account object → Buttons, Links, and Actions → New Action
  • Action Type: Lightning Component
  • Create an LWC or Aura component that:
    1. Calls getChildAccounts to display merge candidates
    2. Lets user select master account
    3. Calls mergeAccounts with selections
    4. Shows success/error messages with Lightning toast notifications

Step 3: Add to Page Layout

  • Edit Account page layout
  • Add the new quick action to the Salesforce Mobile and Lightning Experience Actions section
  • Remove the old JavaScript button

Apex Merge Logic Implementation The Database.merge() method handles child relationships automatically:

  • Reparents child records to master account
  • Preserves data based on merge field selections
  • Maintains audit trail
  • Enforces security and validation rules

Your Apex has better capabilities than JavaScript:

  • Proper exception handling with try-catch
  • Transaction rollback on errors
  • Governor limit management
  • Server-side validation that can’t be bypassed

Migration Timeline Salesforce is removing JavaScript button support entirely in future releases. Plan to migrate all custom buttons to:

  • Lightning quick actions (for complex logic)
  • Lightning flow (for simpler automation)
  • Standard Salesforce features where possible

The conversion takes effort upfront but results in more maintainable, secure, and mobile-friendly functionality. Your users will get better error messages and the merge process will work consistently across desktop and mobile devices.

JavaScript buttons are deprecated in Lightning and won’t execute. The sforce API doesn’t work in Lightning context. You’ll need to convert this to a Lightning quick action with an Apex controller. There’s no workaround to make the old JavaScript work.

Before rebuilding, check if the standard Salesforce merge functionality meets your needs. Lightning Experience has built-in account merge that handles child relationships automatically. If your custom logic is just querying children and validating before merge, the standard feature might work. You can customize the merge page layout to show the fields your users need to compare. Only build custom if you have truly unique business logic.