You need to sign in to do that
Don't have an account?

Need help with SOQL for Trigger on Case to grab info from User for case owner
I am new at sql and soql and need some help. I am trying to find out if a user record is marked out of office and if so add a backup user to their cases. I am having trouble with my query.
My Trigger
trigger myCaseTrigger on Case (before insert, before update) { Case[] cs = Trigger.new; caseChange.makeChange(cs); }
This is working for me as a test to change the case
public class caseChange { public static void makeChange(Case[] cs){ for (Case u : cs ) { if (u.Backup_User__c == null ) { u.Backup_User__c = u.OwnerId;} } } }
The next two are my attempt, but are not working
public class caseChange { public static void makeChange(Case[] cs){ for (Case u : cs ) { if (u.Owner__r.Out_Of_Office__c == 'true' ) { u.Backup_User__c = u.Owner__r.Backup_User__c;} } } }
public class caseChange { public static void makeChange(Case[] cs){ for (User u : cs ) { if (u.Out_Of_Office__c == 'true' ) { u.Cases__r.Backup_User__c = u.OwnerId;} } } }
Thank you for any advice or help.
I'm not exactly sure what you're trying to do, but you cannot join objects without querying the data.
for example, this should work for your first example that is not working:
public class caseChange {
public static void makeChange(Case[] cs){
Set<Id> ownerIds = new Set<Id>();
Map<Id,Id> map1 = new Map<Id,Id>();
Map<Id,Id> map2 = new Map<Id,Id>();
Map<Id,Id> map3 = new Map<Id,Id>();
for (Case c : cs) {
ownerIds.add(c.Owner__c);
map1.put(c.Id,c.Owner__c);
}
for(User u : [select Id, Out_Of_Office__c, Backup_User__c from User where Id in : ownerIds]){
map2.put(u.Id,u.Out_Of_Office__c);
map3.put(u.Id,u.Backup_User__c);
}
for(Case c : cs){
if (map2.get(map1.get(c.Id)) == 'true' ) {
c.Backup_User__c = map3.get(map1.get(c.Id));
}
}
}
}
Hope that helps you rewrite the last one and do whatever you're trying to do...
All Answers
I'm not exactly sure what you're trying to do, but you cannot join objects without querying the data.
for example, this should work for your first example that is not working:
public class caseChange {
public static void makeChange(Case[] cs){
Set<Id> ownerIds = new Set<Id>();
Map<Id,Id> map1 = new Map<Id,Id>();
Map<Id,Id> map2 = new Map<Id,Id>();
Map<Id,Id> map3 = new Map<Id,Id>();
for (Case c : cs) {
ownerIds.add(c.Owner__c);
map1.put(c.Id,c.Owner__c);
}
for(User u : [select Id, Out_Of_Office__c, Backup_User__c from User where Id in : ownerIds]){
map2.put(u.Id,u.Out_Of_Office__c);
map3.put(u.Id,u.Backup_User__c);
}
for(Case c : cs){
if (map2.get(map1.get(c.Id)) == 'true' ) {
c.Backup_User__c = map3.get(map1.get(c.Id));
}
}
}
}
Hope that helps you rewrite the last one and do whatever you're trying to do...
Hello jpwagner,
Thank you for the example I haven't figured it out yet but I am working with your example.
I am trying to have a case trigger check if the case owner's user record has the {!Out_Of_Office__c} user field marked as true, and if true grab the value for the {!Backup_User__c} user field from the case owner's user record and add that as the value of the {!Backup_User__c} case field.
A workflow rule will send an e-mail alert to the case backup user if the case owner is out of office everytime {!Case_Last_Modified} case field is changed. Custom last modified field is also updated when self-service users add/edit new/existing case comments, and IsIncomming and IsOutgoing e-mails, as well as the case being edited.
Thank you.
**found an error
out_of_office__c is boolean right? so map2 should be <id,boolean>
Out_Of_Office__c is boolean, and that got me passed the first error but now i receive error that the following must be compatible types: boolean, string
if (map2.get(map1.get(c.Id)) == 'true' ) { c.Backup_User__c = map3.get(map1.get(c.Id)); }
Thanks again jpwagner,
This is working:
public class caseChange { public static void makeChange(Case[] cs){ Set<Id> ownerIds = new Set<Id>(); Map<Id,Id> map1 = new Map<Id,Id>(); Map<Id,boolean> map2 = new Map<Id,boolean>(); Map<Id,Id> map3 = new Map<Id,Id>(); for (Case c : cs) { ownerIds.add(c.OwnerId); map1.put(c.Id,c.OwnerId); } for(User u : [select Id, Out_Of_Office__c, Backup_User__c from User where Id in : ownerIds]){ map2.put(u.Id,u.Out_Of_Office__c); map3.put(u.Id,u.Backup_User__c); } for(Case c : cs){ if (map2.get(map1.get(c.Id)) == true ) { c.Backup_User__c = map3.get(map1.get(c.Id)); } } } }
Anyone know why I receive this error when I include "before delete" in the trigger?
trigger myCaseTrigger on Case (before delete, before insert, before update) { Case[] cs = Trigger.new; caseChange.makeChange(cs); }
When deleting a case the following error is received:
caseChangeTrigger: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object Class.caseChange.makeChange: line 9, column 17 Trigger.caseChangeTrigger: line 3, column 1
trigger.new is not available in a delete trigger, only in insert and update triggers.
From the online help:
trigger.new Returns a list of the new versions of the sObject records.
Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.