we have Dealer record type page in this vlsalesexecutive lookup(user)field requirment is that particular user which is present in that lookupfield is deacivated automatically clear this lookup field user value on that record page. we write trigger ad callnin after update in user triggerhandler class and logic is wrote on userhelper class.but when we deACIVATE THAT PARTICULAR USER IT VALUE IS NOT BLANK
public static void clearLookupField(List<User> userList) {
Set<Id> userIds = new Set<Id>();
for(User u : userList) {
if(u.IsActive == false) {
userIds.add(u.Id);
}
}
// Perform DML on User object (setup object)
if(!userIds.isEmpty()) {
update new List<User>([SELECT Id FROM User WHERE Id IN :userIds]);
}
// Perform DML on Account object (non-setup object
List<Account> accountsToUpdate = new List<Account>();
for(Account acc : [SELECT Id, VL_Sales_Executive__c FROM Account WHERE VL_Sales_Executive__c IN :userIds]) {
acc.VL_Sales_Executive__c = null;
accountsToUpdate.add(acc);
}
if(!accountsToUpdate.isEmpty()) {
update accountsToUpdate;
System.debug('Accounts updated: ' + accountsToUpdate.size());
}else {
System.debug('No accounts to update');
}
}
In triggerhandler class:
List<User> userList= new List<User>();
UserHelper.clearLookupField(userList);
Hi @Rashmee Kasawar,
Issue seems to be with the trigger handler, where the correct user ids are not being passed to the helper class. Hence, the helper class fails to identify deactivated users and clear the lookup field on related accounts when users are deactivated.
To fix this, update the trigger handler to pass the correct user ids using Trigger.newMap.keySet() and modify the helper class to check if the related user is deactivated using !userIds.contains(acc.VL_Sales_Executive__c) before clearing the lookup field on the account.
Hope this helps you. Thanks