You need to sign in to do that
Don't have an account?
dlCamelot
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'');
}
}
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'');
}
}
The actual problem in your code is there is no DML statement to updated the modified values.
Here is the updated code below Check this Apex trigger trailhead module - https://developer.salesforce.com/trailhead/module/apex_triggers
All Answers
The actual problem in your code is there is no DML statement to updated the modified values.
Here is the updated code below Check this Apex trigger trailhead module - https://developer.salesforce.com/trailhead/module/apex_triggers
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;
}
}
}