• Alex McLean
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hi, I am writing a trigger that checks a checkbox field in the contact object after calculating the primaryContact, The primaryContact is the contact who has the most commissions, there is one primary contact per account. My code is able to chose a primary contact but it doesnt seem to update the value of the checkbox to true? This is what I have so far: 
trigger primaryContact on Commision__c (after insert, after update) {
    List<Account> accToCon = [SELECT Id,
                              (SELECT Id, Name, Total_Commission__c FROM Contacts) FROM Account];
    
    for(Commision__c com : Trigger.New){
        for(Account c: accToCon){
            Contact primaryContact;
            if(!c.Contacts.isEmpty()){
                primaryContact = findMaxCommission(c.Contacts);
                System.debug(primaryContact);
                togglePrimaryContact(c.Contacts, primaryContact.id);
            }
            else{
                continue;
            }
        }
    }
    
    Contact findMaxCommission(List<Contact> contacts){ 
        Contact chosenContact = contacts[0];
        Decimal max = contacts[0].Total_Commission__c;
        for (Contact i : contacts) { 
            if (max < i.Total_Commission__c) { 
                max = i.Total_Commission__c; 
                chosenContact = i;
            } 
        } 
        return chosenContact; 
    } 
    
    void togglePrimaryContact(List<Contact> contacts, id primaryContactId){
        for(Contact i : contacts){
            if(i.id == primaryContactId){
                i.Primary__c = true;
            }
            i.Primary__c = false;
        }
    }
}
any help would be greatly appreciated! thanks
 
Hi, I am trying to create a trigger that will check a primaryContact checbox field on a contact. To do this, I need to calculate who has the most total commissions per account in the system. My approach is th loop thrighn the accounts, then throguh the contacts of each account and caluclate the max total commission then assign the primaryContact checbox to true for this contact and false for all others in the account. This my code so far: 
trigger primaryContact on Commision__c (before insert, before update, before delete) {
    Account acct;
    List<Contact> contacts;
    Map<Id,Account> mapAccount = new Map<Id,Account>([select id from account]);
    Set<ID> accountIds = new Set<ID>();
    accountIds = mapAccount.keySet();
    
    for(Id accountId: accountIds){
        Contact primaryCon;
        getAcctContacts(accountId);
        for(Contact con: contacts){
            System.debug(con);
        }
        if(!contacts.isEmpty()){
            primaryCon = findMaxCommission(contacts);
            togglePrimaryContact(contacts, primaryCon.Id);
            
        }
        else{
            break;
        }
        
        
    }
    
    void getAcctContacts(id id){
        acct = [SELECT Id, Name,
                (SELECT Id, Name, Total_Commission__c FROM Contacts) FROM Account
                where id = :id limit 1];
        contacts = acct.Contacts;
    }
    
    Contact findMaxCommission(List<Contact> contacts){ 
        Contact chosenContact = contacts[0];
        Decimal max = contacts[0].Total_Commission__c;
        for (Contact i : contacts) { 
            if (max < i.Total_Commission__c) { 
                max = i.Total_Commission__c; 
                chosenContact = i; 
            } 
        } 
        return chosenContact; 
    } 
    
    void togglePrimaryContact(List<Contact> contacts, id primaryContactId){
        for(Contact i : contacts){
            i.Primary__c = true;
        }
    }
}

This is the error I get
System.DmlException: Insert failed. First exception on row 0; first error: ENTITY_IS_DELETED, entity is deleted: []

I'm not sure what is the best practice for this type of develoment, any help is appreciated.

 
Hi, I am writing a trigger that checks a checkbox field in the contact object after calculating the primaryContact, The primaryContact is the contact who has the most commissions, there is one primary contact per account. My code is able to chose a primary contact but it doesnt seem to update the value of the checkbox to true? This is what I have so far: 
trigger primaryContact on Commision__c (after insert, after update) {
    List<Account> accToCon = [SELECT Id,
                              (SELECT Id, Name, Total_Commission__c FROM Contacts) FROM Account];
    
    for(Commision__c com : Trigger.New){
        for(Account c: accToCon){
            Contact primaryContact;
            if(!c.Contacts.isEmpty()){
                primaryContact = findMaxCommission(c.Contacts);
                System.debug(primaryContact);
                togglePrimaryContact(c.Contacts, primaryContact.id);
            }
            else{
                continue;
            }
        }
    }
    
    Contact findMaxCommission(List<Contact> contacts){ 
        Contact chosenContact = contacts[0];
        Decimal max = contacts[0].Total_Commission__c;
        for (Contact i : contacts) { 
            if (max < i.Total_Commission__c) { 
                max = i.Total_Commission__c; 
                chosenContact = i;
            } 
        } 
        return chosenContact; 
    } 
    
    void togglePrimaryContact(List<Contact> contacts, id primaryContactId){
        for(Contact i : contacts){
            if(i.id == primaryContactId){
                i.Primary__c = true;
            }
            i.Primary__c = false;
        }
    }
}
any help would be greatly appreciated! thanks
 
Hi, I am trying to create a trigger that will check a primaryContact checbox field on a contact. To do this, I need to calculate who has the most total commissions per account in the system. My approach is th loop thrighn the accounts, then throguh the contacts of each account and caluclate the max total commission then assign the primaryContact checbox to true for this contact and false for all others in the account. This my code so far: 
trigger primaryContact on Commision__c (before insert, before update, before delete) {
    Account acct;
    List<Contact> contacts;
    Map<Id,Account> mapAccount = new Map<Id,Account>([select id from account]);
    Set<ID> accountIds = new Set<ID>();
    accountIds = mapAccount.keySet();
    
    for(Id accountId: accountIds){
        Contact primaryCon;
        getAcctContacts(accountId);
        for(Contact con: contacts){
            System.debug(con);
        }
        if(!contacts.isEmpty()){
            primaryCon = findMaxCommission(contacts);
            togglePrimaryContact(contacts, primaryCon.Id);
            
        }
        else{
            break;
        }
        
        
    }
    
    void getAcctContacts(id id){
        acct = [SELECT Id, Name,
                (SELECT Id, Name, Total_Commission__c FROM Contacts) FROM Account
                where id = :id limit 1];
        contacts = acct.Contacts;
    }
    
    Contact findMaxCommission(List<Contact> contacts){ 
        Contact chosenContact = contacts[0];
        Decimal max = contacts[0].Total_Commission__c;
        for (Contact i : contacts) { 
            if (max < i.Total_Commission__c) { 
                max = i.Total_Commission__c; 
                chosenContact = i; 
            } 
        } 
        return chosenContact; 
    } 
    
    void togglePrimaryContact(List<Contact> contacts, id primaryContactId){
        for(Contact i : contacts){
            i.Primary__c = true;
        }
    }
}

This is the error I get
System.DmlException: Insert failed. First exception on row 0; first error: ENTITY_IS_DELETED, entity is deleted: []

I'm not sure what is the best practice for this type of develoment, any help is appreciated.