Skip to main content
Fixing code formatting, adding some blank lines to help readability
Source Link
Derek F
  • 64.1k
  • 15
  • 55
  • 104
trigger SetPrimary on Commission__c (after insert, after update, after delete) {
//This gets all the account ids associated with the Commission records
Set<Id> accountIds = new Set<Id>();
for (Commission__c c : Trigger.new) {
    accountIds.add(c.Account__c);
}
//This should query the database for all the Contact records related to the accounts with commission records
 Map<Id, Contact> contacts = new Map<Id, Contact>([SELECT Id, AccountId, Total_Commission__c FROM Contact WHERE AccountId IN :accountIds]);

// Loops through each Account
for (Id accountId : accountIds) {
    // Finds the Contact with the highest Total Commission
    Contact primaryContact = null;
    for (Contact c : contacts.values()) {
        if (c.AccountId == accountId) {
            if (primaryContact == null || c.Total_Commission__c > primaryContact.Total_Commission__c) {
                primaryContact = c;
            }
        }
    }
    
    // Sets the Primary field to true for the primary Contact and false for all others
    for (Contact c : contacts.values()) {
        if (c.AccountId == accountId) {
            c.Primary__c = (c.Id == primaryContact.Id);
        }
    }
}
update contacts.values();}
trigger SetPrimary on Commission__c (after insert, after update, after delete) {
    //This gets all the account ids associated with the Commission records
    Set<Id> accountIds = new Set<Id>();
    for (Commission__c c : Trigger.new) {
        accountIds.add(c.Account__c);
    }

    //This should query the database for all the Contact records related to the accounts with commission records
     Map<Id, Contact> contacts = new Map<Id, Contact>([SELECT Id, AccountId, Total_Commission__c FROM Contact WHERE AccountId IN :accountIds]);

    // Loops through each Account
    for (Id accountId : accountIds) {
        // Finds the Contact with the highest Total Commission
        Contact primaryContact = null;

        for (Contact c : contacts.values()) {
            if (c.AccountId == accountId) {
                if (primaryContact == null || c.Total_Commission__c > primaryContact.Total_Commission__c) {
                    primaryContact = c;
                }
            }
        }
        
        // Sets the Primary field to true for the primary Contact and false for all others
        for (Contact c : contacts.values()) {
            if (c.AccountId == accountId) {
                c.Primary__c = (c.Id == primaryContact.Id);
            }
        }
    }

    update contacts.values();
}

Total_Commission_cTotal_Commission__c is a field in the ContactContact object (roll-up summary)

trigger SetPrimary on Commission__c (after insert, after update, after delete) {
//This gets all the account ids associated with the Commission records
Set<Id> accountIds = new Set<Id>();
for (Commission__c c : Trigger.new) {
    accountIds.add(c.Account__c);
}
//This should query the database for all the Contact records related to the accounts with commission records
 Map<Id, Contact> contacts = new Map<Id, Contact>([SELECT Id, AccountId, Total_Commission__c FROM Contact WHERE AccountId IN :accountIds]);

// Loops through each Account
for (Id accountId : accountIds) {
    // Finds the Contact with the highest Total Commission
    Contact primaryContact = null;
    for (Contact c : contacts.values()) {
        if (c.AccountId == accountId) {
            if (primaryContact == null || c.Total_Commission__c > primaryContact.Total_Commission__c) {
                primaryContact = c;
            }
        }
    }
    
    // Sets the Primary field to true for the primary Contact and false for all others
    for (Contact c : contacts.values()) {
        if (c.AccountId == accountId) {
            c.Primary__c = (c.Id == primaryContact.Id);
        }
    }
}
update contacts.values();}

Total_Commission_c is a field in the Contact object (roll-up summary)

trigger SetPrimary on Commission__c (after insert, after update, after delete) {
    //This gets all the account ids associated with the Commission records
    Set<Id> accountIds = new Set<Id>();
    for (Commission__c c : Trigger.new) {
        accountIds.add(c.Account__c);
    }

    //This should query the database for all the Contact records related to the accounts with commission records
     Map<Id, Contact> contacts = new Map<Id, Contact>([SELECT Id, AccountId, Total_Commission__c FROM Contact WHERE AccountId IN :accountIds]);

    // Loops through each Account
    for (Id accountId : accountIds) {
        // Finds the Contact with the highest Total Commission
        Contact primaryContact = null;

        for (Contact c : contacts.values()) {
            if (c.AccountId == accountId) {
                if (primaryContact == null || c.Total_Commission__c > primaryContact.Total_Commission__c) {
                    primaryContact = c;
                }
            }
        }
        
        // Sets the Primary field to true for the primary Contact and false for all others
        for (Contact c : contacts.values()) {
            if (c.AccountId == accountId) {
                c.Primary__c = (c.Id == primaryContact.Id);
            }
        }
    }

    update contacts.values();
}

Total_Commission__c is a field in the Contact object (roll-up summary)

Source Link
Rad
  • 3
  • 2

Primary user Checkbox ticked for Contact with highest Commission

I'm new to Salesforce and Apex. I need help setting a trigger which checks Contacts under a specific Account. The Contact with the highest Total Commission value is marked as Primary user by ticking the checkbox (and the other contacts are unticked).

Currently I have this code and it seems to work but not always, sometimes it doesn't tick the Contact with highest commission and I dont know why.

trigger SetPrimary on Commission__c (after insert, after update, after delete) {
//This gets all the account ids associated with the Commission records
Set<Id> accountIds = new Set<Id>();
for (Commission__c c : Trigger.new) {
    accountIds.add(c.Account__c);
}
//This should query the database for all the Contact records related to the accounts with commission records
 Map<Id, Contact> contacts = new Map<Id, Contact>([SELECT Id, AccountId, Total_Commission__c FROM Contact WHERE AccountId IN :accountIds]);

// Loops through each Account
for (Id accountId : accountIds) {
    // Finds the Contact with the highest Total Commission
    Contact primaryContact = null;
    for (Contact c : contacts.values()) {
        if (c.AccountId == accountId) {
            if (primaryContact == null || c.Total_Commission__c > primaryContact.Total_Commission__c) {
                primaryContact = c;
            }
        }
    }
    
    // Sets the Primary field to true for the primary Contact and false for all others
    for (Contact c : contacts.values()) {
        if (c.AccountId == accountId) {
            c.Primary__c = (c.Id == primaryContact.Id);
        }
    }
}
update contacts.values();}

Total_Commission_c is a field in the Contact object (roll-up summary)