+ Start a Discussion
mxalix258mxalix258 

Trigger to update user

I'm having trouble creating a basic trigger to update a user record. Basically, we have an object that has a lookup to a user. When the object meets a certain criteria I would like to deactivate the associated User. Here is what I've come up with so far that doesn't seem to be working:

 

Trigger:

 

trigger deactivateuser on On_Off_Boarding__c (before update) {

	List<On_Off_Boarding__c> oblist = Trigger.new;

	for(On_Off_Boarding__c ob : oblist){
		
		if(ob.Last_Day__c == date.today()){

		DeactivateUser user = new DeactivateUser (ob);

		user.deactivateUser();

		}

	}

}

 

Class

public class DeactivateUser {

private On_Off_Boarding__c obAttribute;

//Constructor that accepts a On/Offboarding record
public DeactivateUser(On_Off_Boarding__c constructorob){

    obAttribute = constructorob;

}

@future
static public void deactivateUser(){

    if(obAttribute != null){
        User u = new User();
        u = [Select ID, Name, IsActive FROM User Where ID =:obAttribute.Associated_User__c];
        u.IsActive=False;

    }
    
Database.update(userAttribute);

}
}

 

Where am I going wrong? When trying to create the class, I get the "obAttribute.Associated_User__c variable does not exist".

 

Thanks!

bob_buzzardbob_buzzard
obAttribute is private, which means that it is only accessible inside instances of this class.

The deactivateUser method is static, which means that its associated with the class itself, rather than any instance.

Thus as far as the method is concerned, there is no variable of that name as its not visible in a static context.