function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Patrick Maxwell AppfolioPatrick 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;
 
}

Ricky MartinRicky Martin
Hi,
If you wish sahre your credential, I will try to resolve.
Ramu_SFDCRamu_SFDC
Following are my observations

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.