You need to sign in to do that
Don't have an account?
Alex McLean
How to change a field of an object after inserting another object?
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
Alex,
You have several issues here. Primarily, since the trigger is on Commission__c and you're wanting to update the affected Contact records, you need to issue a DML statement on the Contact records that are changing (aka. update(contacts)).
However, you're solution isn't very scaleable -- You're processing ALL accounts and ALL their contacts, even if the commission didn't change. You should probably get a list of accounts (or contacts) from the Commission object and only query those affected records to begin with.
When changing the primary contact, you should really ensure that you're making a change and NOT issue the update if it's the same contact that was already primary.
NOTE: Untested code. Use/modify at your discression and own the risk:
This example assumes there is an account lookup field on the commission__c object called Account__c.
I suggest using a map for other records to update like Contact records in this scenario. This will ensure that you can only list a single Contact once in the list of records to update. It's probably not needed in this example because each contact should only be evaluated as the primary contact 1 time, but it's a handy way to prevent issues.
Thank you for your quick and extremely informative reply. Your solution makes alot of sense and I can see exactly where I was going wrong and some bad practices that I was applying. Your kindess goes a long way and is very much appreciated. Thank you