Skip to main content

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:







Buy me a coffeeBuy me a coffee

Comments

Popular posts from this blog

How to show Current User Profile Picture in Lightning Component

Hello frnds today post learn how to show current user profile picture in lightning component so let us start....... Step=>1.            goto developer console and create lightning component               File=>New=>Lighning Component                DisplayUserPhoto.cmp <aura:component controller="LoginUserProfileCtrl"                 implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction"                 access="global" >     <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>     <aura:attribute name="oUser" type="user" default="{'sobjectType' : 'User'}" />     <h1>Current User Profile Pict...

How to Solve Mixed DML Error in APEX Test class #Salesforce

Hello frinds, Today i am talking about , how to solve mixd DML error in Apex, This error mostly come when , yiu are writing Apex Test . When are you insert Setup and non-setup object records in single transaction. Step-1- Writing Apex test class @isTest public class MIXEDDML { @isTest public static void TestData(){ UserRole obj=new UserRole(Name= 'ABC'); insert obj; Profile pf= [Select Id from profile where Name='System Administrator']; String orgId=UserInfo.getOrganizationId(); String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ; User uu=new User(firstname = 'ABC', lastName = 'XYZ', email = 'ak288@test.org', Username = 'ak288@test.org', EmailEncodingK...

Create Overlays with the New Modal Component

Hello friends, Now modal tag is available in #LWC, we can create a modal using the #LWC Standard tag. snapshot given below with code. Step-1 Create LWC component Step-2 Add below code Step-3 I am showing how my component is look like. Thanks for reading