+ Start a Discussion
Jayaramu T 9Jayaramu T 9 

Can you please any one help on this? Please find the below is my code and PFA. based on if Active field unchecked in user object related contact object "Manage external user " disable the partner user

User-added imageUser-added image
global class BatchDeactivateUsers implements Database.Batchable<sobject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        Map<String,daystodeactivateuser__c> csdaystodeactivateuser = daystodeactivateuser__c.getAll();
        integer noOfDays=0;
        if(csdaystodeactivateuser!=null){
            noOfDays = integer.ValueOf(csdaystodeactivateuser.get('numberofdaysemailnotificationsent').Numberofdays__c);
        }
        string query = 'SELECT Id,isActive,LastLoginDate,UserDeactivationDate__c FROM User where isActive=true and UserDeactivationDate__c!=null and UserDeactivationDate__c = TODAY and lastlogindate < LAST_N_DAYS:'+noOfDays;
        if (!Test.IsRunningTest()) {
            return Database.getQueryLocator(query);
        } else {
            return Database.getQueryLocator('SELECT Id,isActive,LastLoginDate,UserDeactivationDate__c FROM User limit 1');
        }
    }
    global void execute(Database.BatchableContext BC, List<user> userList) {
        for (User usrRec: userList) {
            usrRec.isActive = false;
            if(usrRec.contactId!=null){
                usrRec.isPortalenabled = false;  
            }
        }
        if (userList.size() > 0 && !Test.isRunningTest()) {
            update userList;
        }
    }
    global void finish(Database.BatchableContext BC) {}
}