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
ARE youARE you 

Setting User.IsPortalEnabled in Apex Class not working

Hi, 
I'm facing the following Issue: I need to deactivate portal Users in a Apex class programmatically. 
I already read: https://developer.salesforce.com/forums/?id=9060G000000UUQyQAO, but it is still not working. Here is my code:
 
User user = [SELECT Id FROM User WHERE Contact.AccountId = :accountId];
// fetch Users Id
 Id uid = user.Id;
user.IsActive = false;
user.IsPortalEnabled = false;
System.debug('IsPortalEnabled before saving: ' + user.IsPortalEnabled);
System.debug('IsActive before saving: ' + user.IsActive);
update user;
// re-fetch User Object
User us = [SELECT Name, IsPortalEnabled, IsActive FROM USER WHERE Id = :user.Id];
System.debug('IsPortalEnabled after saving: ' + us.IsPortalEnabled);
System.debug('IsActive after saving:' + us.IsActive);

Even by setting IsPortalEnabled to false before saving if I re-query the same User Object the Flag will be reset. The change of IsActive is stored as expected.

Here is the log output of my Test-Class:

console log

Anybody an Idea why my code is not working?

Thanks in advance
ARE youARE you
Short Update on that: It seem to be working in General, Problem is it isn't be doable programmatically in an Apex Class. There seems to be a significant time lag on the change of the Person Account and the User - even if the single parts of my code works the changes are not already done and this causes the problem - still no solution for it.