You need to sign in to do that
Don't have an account?
Alex McLean
Toggling checkbox field by calculating total commissions trigger
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:
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.
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.
trigger primaryContact on Commision__c (AFTER insert, AFTER update, AFTER delete
2.it is not the best practice to query the whole account object on the line
Map<Id,Account> mapAccount = new Map<Id,Account>([select id from account]);
you will come across query 50001 Governor limit error if you have more than 50000 account record.
Use the relationship between commissioned and account or contact to filter the query
3. Method getAcctContacts(id) contains a soql query and this method is used in a loop. Salesforce suggests not put soql in a loop.
All Answers
Greetings!
This error occurs when you are trying to refer the delete record and the mapping is incorrect.
I would suggest you to add the debug statements to understand the code execution which will help you in narrow downing the issue.
Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.
Warm Regards,
Shirisha Pathuri
trigger primaryContact on Commision__c (AFTER insert, AFTER update, AFTER delete
2.it is not the best practice to query the whole account object on the line
Map<Id,Account> mapAccount = new Map<Id,Account>([select id from account]);
you will come across query 50001 Governor limit error if you have more than 50000 account record.
Use the relationship between commissioned and account or contact to filter the query
3. Method getAcctContacts(id) contains a soql query and this method is used in a loop. Salesforce suggests not put soql in a loop.
How can I change it so the primary contact checkbox in contacts is checked? the code is returning the primary contact information correctly.. thank you