Reply
Regular Contributor
mxalix258
Posts: 69
0

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!

Moderator
bob_buzzard
Posts: 6,460
0

Re: Trigger to update user

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.
--
Certified Salesforce Technical Architect, Developer, Advanced Developer, Administrator, Advanced Administrator, Consultant, Sales Cloud Consultant,Service Cloud Consultant
Force.com MVP | The Bob Buzzard Blog | Linked In | Twitter

I don't respond to private messages/emails asking for help.


Take the Bob Buzzard Sobject Fields quiz.