• shashank rajawat
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Trying to write a trigger purely to do the following:
WHEN case created (not edited)
THEN Case.Case_Owner__c (lookup field) overwrites OwnerID field.

Also need to include any catch exceptions in the code (just to keep best practices)

This is not working.  Any help?
trigger UpdateCaseOwner on Case (after insert) {
  // create a set of all the unique ownerIds
  Set<id> ownerIds = new Set<id>();
  //Identifying which records need data grabbed 
    for (Case c : Trigger.new)
      ownerIds.add(c.OwnerId);
    
  //Grabbing the data from those records
  Map<id, Case> owners = new Map<id, Case>([Select Case_Owner__c from Case Where Id in :ownerIds]); 

  //Adding that data back into the original records
      for (Case c : Trigger.new)
           try {
            c.OwnerID = owners.get(c.OwnerId).Case_Owner__c;
            }
            catch (System.NullPointerException e) {
            System.debug('Null pointer exception''); 
        }
}