You need to sign in to do that
Don't have an account?
Patrick Maxwell Appfolio
Apex Trigger to update field across objects upon update or insert
I recently wrote my first trigger and I'll just say now that I'm not a formally trained software developer. By researching online I was abe to bulkify my code and it seems to be working great in Sandbox with either a singular update or with Data Loader, however I'm sure that I've done something wrong, so I wanted to throw my work to the sharks and see what I have done incorrectly before I start trying to learn how to write test classes and push to Production. Thank you very much for your time.
trigger MyCaseCustomerStatusUpdate on Vertical__c (after insert, after update) {
// create a set of related account IDs
set <ID> accountIDs = new set <ID>();
//create set of all verticals
set <Vertical__c> verticals = new set <Vertical__c>();
//create list of accounts to update at end of code
list <Account> accountList = new list <Account>();
//create Account object for later reference
Account accountRecord;
// for every Vertical, add its related to account to the set and create set of Verticals for later reference
for(Vertical__c v: Trigger.new){
accountIDs.add(v.account__c);
verticals.add(v);
}
// create a Map to match the Vertical related to Account IDs to their corresponding account
Map<ID, Account> accountMap = new Map<ID, Account> ([Select ID FROM Account WHERE ID in :accountIDs]);
//loop through all accounts added to account map
for(Account a:accountMap.values()){
//add account to list to update
accountList.add(a);
//loop through verticals
for(Vertical__c v : verticals){
//if the ID on the account and the account ID on the vertical match, update the status field.
if (a.Id == v.account__c){
// update Account object
accountRecord = a;
// update the status on the account object
accountRecord.MyCase_Status__c = v.Status__c;
}
}
}
update accountList;
}
trigger MyCaseCustomerStatusUpdate on Vertical__c (after insert, after update) {
// create a set of related account IDs
set <ID> accountIDs = new set <ID>();
//create set of all verticals
set <Vertical__c> verticals = new set <Vertical__c>();
//create list of accounts to update at end of code
list <Account> accountList = new list <Account>();
//create Account object for later reference
Account accountRecord;
// for every Vertical, add its related to account to the set and create set of Verticals for later reference
for(Vertical__c v: Trigger.new){
accountIDs.add(v.account__c);
verticals.add(v);
}
// create a Map to match the Vertical related to Account IDs to their corresponding account
Map<ID, Account> accountMap = new Map<ID, Account> ([Select ID FROM Account WHERE ID in :accountIDs]);
//loop through all accounts added to account map
for(Account a:accountMap.values()){
//add account to list to update
accountList.add(a);
//loop through verticals
for(Vertical__c v : verticals){
//if the ID on the account and the account ID on the vertical match, update the status field.
if (a.Id == v.account__c){
// update Account object
accountRecord = a;
// update the status on the account object
accountRecord.MyCase_Status__c = v.Status__c;
}
}
}
update accountList;
}
If you wish sahre your credential, I will try to resolve.
1. Update accountlist - this statement will update account however there will be no changes to the account record as you have just added the unchanged account records.
2. As vertical is a child record to Account, there could be multiple vertical accounts for a single Account in that case the Account status can be the status of any of these verticals. For example an account record abc has 5 vertical accounts with various statuses. When you try to update the status of the Account it gets updated 5 times and the status can be of any vertical account depending on which status was last updated.
If you want to resolve this situation, please plan on the second point so that I can help you on the code.