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
MiguelGuerreiroMiguelGuerreiro 

checking if "future" owner is in the office

hello,

 

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!

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

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

 

Thank you!

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

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

MiguelGuerreiroMiguelGuerreiro

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.
werewolfwerewolf

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.

MiguelGuerreiroMiguelGuerreiro

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)!!!

 

werewolfwerewolf
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.
MiguelGuerreiroMiguelGuerreiro

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.