+ Start a Discussion

checking if "future" owner is in the office



I have a user field (checkbox) that indicates if the users is Out of the Office. 


Is it possible that when a user assign a object (case) to that user the system would prevent this (via validation rule or with another method?


Maybe an trigger?


thank you!

Assignment rules do not take availability into account (or any user field really) when assigning.  You could do that with a trigger though.

I don't have any experience with triggers... Can you point me to some help docs? Or code sample?


Thank you!

Help docs can be found here.  Sample code is here.
very nice! Thank you :smileyhappy:

OK, I've been trying to do this but without success.


I created a class to check if a user is in the office or not. But in the trigger itself I can't manage to pass the user (owner of the case) to the function. I'm not even sure if the logic is correct.

Can anyone give me a hand on how to get the user of the case I'm updating?


thank you


trigger ownerOutOfOffice on Case (after update) { Map<Boolean, User> uMap = new Map<Boolean, User>(); User u = [SELECT Id, Out_of_Office__c FROM User WHERE Id = '005800000026lD0']; uMap.put(u.Out_of_Office__c, u); if ((Trigger.isUpdate)&&(UserOutOfOffice.checkUser(u))) { for (Case cas : Trigger.new) { cas.addError('Cannot update owner'); } } else{} }


This is the code I have... but I just can't get the user to pass in my function.

What do you mean, you can't get the user to pass in your function?


Anyway if you're looking to change the case you probably want to do it on before update.  After update it's already settled.


Well this is my code now



trigger ownerOutOfOffice on Case (before update) { //create array of cases being updated Case[] cases = Trigger.new; //get the first case Case cas = cases[0]; // create user User usr; try { usr = [SELECT Out_of_Office__c FROM user WHERE Id =:cas.OwnerId]; if (usr.Out_of_Office__c) cas.addError('Cannot Update Owner'); else{} } catch(System.Queryexception e) { cas.addError('OOOOPS...'); } }


 It seems to be working as intended but if you detect any obvious flaw let me know!


I'm really new to apex and coding in general so stupid mistakes happen (like the after update)!!!


That seems fine, although it will fail if more than one case is filed at once.  Well, not fail exactly, but it will only send the out of office on the first case.

yeah you're right... But my users can only transfer cases one by one (this ok) or mass transfer to one user (not so ok but works because it's always the same user).


fyi my goal is to prevent users that are out of the office from receiving new cases.