How to Create Trigger to update User if Account Type "Prospect" then deactivate User otherwise Active
Hello friends today post , create a trigger on Account, if account record type change in "prospect" then user deactivate and other type activate user ....
so let us start ........
Step=>1. Create a trigger on Account
goto developer console , File=>New=>trigger
updateUser on Account
trigger name:- updateUser.apxt
trigger updateUser on Account (after update) {
Set<Id>allId=new Set<Id>();
for(Account a:Trigger.new){
if(a.Type=='Prospect'){
allId.add(a.Id);
deactivateHelper.updateUser(allId);
}
}
}
Step=>2.
create a Future method of Apex class
goto File=>New=>Apex Class
Apex class name:- deactivateHelper
global class deactivateHelper {
@future
public static void updateUser(Set<Id>allId){
System.debug('result'+allId);
Contact clst=[SELECT Id,Name,Email from Contact WHERE AccountId=:allId];
System.debug('Contact deatails=>'+clst);
List<User>userListToDeactivate=new List<User>();
userListToDeactivate = [SELECT Id,Name from User where username=:clst.Email];
System.debug('User details=>'+userListToDeactivate);
if(userListToDeactivate.size()>0){
for(User u : userListToDeactivate){
u.IsActive = false;
}
}
System.debug('User=>'+userListToDeactivate);
update userListToDeactivate;
}
}
Note: @future is a annotation of future method.
Output:
so let us start ........
Step=>1. Create a trigger on Account
goto developer console , File=>New=>trigger
updateUser on Account
trigger name:- updateUser.apxt
trigger updateUser on Account (after update) {
Set<Id>allId=new Set<Id>();
for(Account a:Trigger.new){
if(a.Type=='Prospect'){
allId.add(a.Id);
deactivateHelper.updateUser(allId);
}
}
}
Step=>2.
create a Future method of Apex class
goto File=>New=>Apex Class
Apex class name:- deactivateHelper
global class deactivateHelper {
@future
public static void updateUser(Set<Id>allId){
System.debug('result'+allId);
Contact clst=[SELECT Id,Name,Email from Contact WHERE AccountId=:allId];
System.debug('Contact deatails=>'+clst);
List<User>userListToDeactivate=new List<User>();
userListToDeactivate = [SELECT Id,Name from User where username=:clst.Email];
System.debug('User details=>'+userListToDeactivate);
if(userListToDeactivate.size()>0){
for(User u : userListToDeactivate){
u.IsActive = false;
}
}
System.debug('User=>'+userListToDeactivate);
update userListToDeactivate;
}
}
Note: @future is a annotation of future method.
Output:
Buy me a coffee
Comments
Post a Comment