function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jayaramu T 9Jayaramu T 9 

i need to update contact field called Contact_active__c=false when User field 'isActive = false' . Can any one please help me on this

Raj VakatiRaj Vakati
Use this code
List<Contact> cons =[SELECT Id, Name,Contact_active__c FROM Contact where Owner.IsActive =False AND Contact_active__c=False  ] ; 
for(Contact c :cons){
	c.Contact_active__c = false ; 
}
update cons ;

 
nitin sharma 356nitin sharma 356
You cas easily do this using Process builder and Flows .
Jayaramu T 9Jayaramu T 9
@Raj Venkak1 follwoing is my code i need to update this code in batch class

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;
        }
    }
Raj VakatiRaj Vakati
give me complete batch class and that is easy for me
Jayaramu T 9Jayaramu T 9
@Raj Vakat1 this is my complete batch class

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) {}
}
Raj VakatiRaj Vakati
Try this code
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 ,ContactIc 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,ContactIc FROM User limit 1');
        }
    }
    global void execute(Database.BatchableContext BC, List<user> userList) {
		Set<Id> contactIds = new Set<Id>();
        for (User usrRec: userList) {
           if( usrRec.isActive = false){
			if(usrRec.contactId!=null){
				contactIds.add(usrRec.contactId);
            }
		   }
        }
        if (contactIds.size() > 0 && !Test.isRunningTest()) {
            List<Contact> cons = [Select Id,Contact_active__c from Contact where Id In :contactIds];
			for(Contact c :cons){
			c.Contact_active__c = false ;
			}
        }
    }
    global void finish(Database.BatchableContext BC) {}
}

 
Jayaramu T 9Jayaramu T 9
Thanks Raj, in this code where we are updating user? actually we are changing user IsActive field based on that related contact should be update.