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
dlCamelotdlCamelot 

Trigger Help - Update OwnerID field on Case

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''); 
        }
}

 
Best Answer chosen by dlCamelot
@Karanraj@Karanraj
You can easy do this with Process builder and workflow rule in salesforce without writing code. Check this Trailhead module to know more about process automation in salesforce. https://developer.salesforce.com/trailhead/module/business_process_automation

The actual problem in your code is there is no DML statement to updated the modified values.
Here is the updated code below
trigger UpdateCaseOwner on Case (after insert) {
   List<Case> updateCaseList = new List<case>();
   //Adding that data back into the original records
   for(Case c : Trigger.new){
     if(c.Case_Owner__c != Null){
       c.OwnerID = c.Case_Owner__c;
       updateCaseList.add(c);
     }
   }
   if(updateCase.size() > 0)
     update updateCaseList;       
}
Check this Apex trigger trailhead module - https://developer.salesforce.com/trailhead/module/apex_triggers
 

All Answers

@Karanraj@Karanraj
You can easy do this with Process builder and workflow rule in salesforce without writing code. Check this Trailhead module to know more about process automation in salesforce. https://developer.salesforce.com/trailhead/module/business_process_automation

The actual problem in your code is there is no DML statement to updated the modified values.
Here is the updated code below
trigger UpdateCaseOwner on Case (after insert) {
   List<Case> updateCaseList = new List<case>();
   //Adding that data back into the original records
   for(Case c : Trigger.new){
     if(c.Case_Owner__c != Null){
       c.OwnerID = c.Case_Owner__c;
       updateCaseList.add(c);
     }
   }
   if(updateCase.size() > 0)
     update updateCaseList;       
}
Check this Apex trigger trailhead module - https://developer.salesforce.com/trailhead/module/apex_triggers
 
This was selected as the best answer
dlCamelotdlCamelot
This almost worked. However, now I'm realizing that I can't use 'AFTER INSERT'.  Is there a way to get this same outcome with BEFORE INSERT?
dlCamelotdlCamelot
Never mind, easy fix :)
shashank rajawatshashank rajawat
I am also facing same issue please update on that
和史 中尾 9和史 中尾 9

In before Insert, you don't need to update records.(They haven't been Inserted yet.)
Thus, this code below will work

trigger UpdateCaseOwner on Case (before insert) {
   List<Case> updateCaseList = new List<case>();
   //Adding that data back into the original records
   for(Case c : Trigger.new){
     if(c.Case_Owner__c != Null){
       c.OwnerID = c.Case_Owner__c;
     }
   }
}