+ Start a Discussion
Akis AthanasiadisAkis Athanasiadis 

Update Custom Field in Users Object

Hi,

I am trying to create a trigger which will be checking a checkbox automatically when another process is done.
In specific, I have a custom object Vacation Requests which follows an approval process. If the request is approved, a checkbox "Approved" is being checked.
Then I am creating a trigger which needs to check if the current date is = to the start date of the vacation. if yes, then the checkbox should be selected.
However this doesn't happen.
Any ideas?
trigger Vacation on Vacation_Requests__c (before insert) {
    User u = new User();
    for (Vacation_Requests__c vr:Trigger.new) {
        if (vr.Approved__c==true && vr.Start_Date__c==System.Today()) 
            u.On_Vacation__c=true;        
    }

}

 
Best Answer chosen by Akis Athanasiadis
HARSHIL PARIKH AKA:GovindHARSHIL PARIKH AKA:Govind
Hi Akis, first, I would recommend this as an After trigger and not before since you only want to update User record After the Vacation object record is SAVED as APPROVED.

I am also thinking that it should be After Update since when user submits Vacation record, its After insert and someone has to approve the record which is AFTER UPDATE. Isn't it?! Let me know your thoughts..

Your Trigger Code can be something along these lines:
 
// This is not bulkified yet but of course, you want to deploy it after bulkified.
trigger Vacation on Vacation_Requests__c (After Update) {
    User u = [Select Id FROM User WHERE Id=: UserInfo.getUserId()];
    for (Vacation_Requests__c vr:Trigger.new) 
    {
        if (vr.Approved__c==true && vr.Start_Date__c==System.Today()) {
             u.On_Vacation__c=true;
           }          
    }
   update u;

}
If this helps or solves the question then please mark it as Best Answer!
 

All Answers

Akis AthanasiadisAkis Athanasiadis
I tested it and instead of the last line 
u.On_Vacation__c=true;

i used =>
vr.addError('Bazinga');

In that case the trigger is trigged while on approving the request. So it seems the issue comes when trying to update a field in another object.
Ideas?!?!?!?​
Akis AthanasiadisAkis Athanasiadis
I understand that an association is missing here. The association between the request owner(user) and the user.
Any ideas on how to connect them?
HARSHIL PARIKH AKA:GovindHARSHIL PARIKH AKA:Govind
Hi Akis, first, I would recommend this as an After trigger and not before since you only want to update User record After the Vacation object record is SAVED as APPROVED.

I am also thinking that it should be After Update since when user submits Vacation record, its After insert and someone has to approve the record which is AFTER UPDATE. Isn't it?! Let me know your thoughts..

Your Trigger Code can be something along these lines:
 
// This is not bulkified yet but of course, you want to deploy it after bulkified.
trigger Vacation on Vacation_Requests__c (After Update) {
    User u = [Select Id FROM User WHERE Id=: UserInfo.getUserId()];
    for (Vacation_Requests__c vr:Trigger.new) 
    {
        if (vr.Approved__c==true && vr.Start_Date__c==System.Today()) {
             u.On_Vacation__c=true;
           }          
    }
   update u;

}
If this helps or solves the question then please mark it as Best Answer!
 
This was selected as the best answer
Akis AthanasiadisAkis Athanasiadis
Thank you!
this worked!!!!!
Akis AthanasiadisAkis Athanasiadis
Hi @Harshil,

What about if I want the checkbox to be automatically unchecked past the vacation end date?
I have used this=>
 
trigger User_On_Vacation on Vacation_Requests__c (Before Update,After Update) {
    User u = [Select Id,LastLoginDate FROM User WHERE Id=: UserInfo.getUserId()];
    for (Vacation_Requests__c vr:Trigger.new) 
    {
        if (vr.Approved__c==true && vr.Start_Date__c==System.Today()) {
             u.On_Vacation__c=true;
           }
        if (vr.End_Date__c < System.Today()) {
             u.On_Vacation__c=false;
           } 
    }
   update u;

}

but it didn't trigger