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
AndrewNerneyAndrewNerney 

Code to update Contact via Execute Anonymous Window not producing results

I have created a field on the Contact object Active_Community_User__c, to be checked when we set up a Community user, and unchecked when we deactivate the Community user.

I need to update this field on existing Community users. Logically that would be best done through the Execute Anonymous window, but the code I have written isn't producing results.

The code appears to execute without error, but nothing changes.

Help?
 
List<User> userList = [ select user.Id, user.Contact.Active_Community_User__c from user where user.IsActive = True and (user.Profile.Name ='zConnect Community User' or user.Profile.Name = 'zConnect Community User Login')]; 

for(User us : userList) { 
    us.Contact.Active_Community_User__c = True; 
} 

update userList;

 
Best Answer chosen by AndrewNerney
TechingCrewMattTechingCrewMatt
Hello, Andrew. You're updating the list of Users, but checking for a change to the Contact record. Please try the following:
 
List<User> userList = [ 
    SELECT 	Id, ContactId 
    FROM 	User 
    WHRE 	IsActive = TRUE AND 
    		ContactId != NULL AND
    		(Profile.Name ='zConnect Community User' OR Profile.Name = 'zConnect Community User Login')
]; 
List<Contact> userContactsToUpdate = new List<Contact>();
for(User user : userList) { 
    userContactsToUpdate.add(
    	new Contact(
        	Id = user.ContactId,
            Active_Community_User__c = true
        )
    );
} 
update userContactsToUpdate;

Hopefully that helps,
Matt Kowalski
 

All Answers

TechingCrewMattTechingCrewMatt
Hello, Andrew. You're updating the list of Users, but checking for a change to the Contact record. Please try the following:
 
List<User> userList = [ 
    SELECT 	Id, ContactId 
    FROM 	User 
    WHRE 	IsActive = TRUE AND 
    		ContactId != NULL AND
    		(Profile.Name ='zConnect Community User' OR Profile.Name = 'zConnect Community User Login')
]; 
List<Contact> userContactsToUpdate = new List<Contact>();
for(User user : userList) { 
    userContactsToUpdate.add(
    	new Contact(
        	Id = user.ContactId,
            Active_Community_User__c = true
        )
    );
} 
update userContactsToUpdate;

Hopefully that helps,
Matt Kowalski
 
This was selected as the best answer
AndrewNerneyAndrewNerney
That's great! I figured it might be something to that end. I'll try this straight away.
AndrewNerneyAndrewNerney

Matt that code worked except for a typo and some stray tabs. Here it is in final format:

List<User> userList = [SELECT Id, ContactId FROM User WHERE IsActive = TRUE AND ContactId != NULL 
                       AND (Profile.Name ='zConnect Community User' OR Profile.Name = 'zConnect Community User Login')];
List<Contact> userContactsToUpdate = new List<Contact>();
for(User user : userList) {
    userContactsToUpdate.add(
    	new Contact(
        	Id = user.ContactId,
            Active_Community_User__c = true
        )
    );
}
update userContactsToUpdate;
TechingCrewMattTechingCrewMatt
Glad to hear it!