Surprise... I did a bit of debug and found out if (u.ContactId == null ) returns false in after update trigger. Thus you cannot use this field to determine if the Customer/Partner user is disabled or not.
I believe once the user is disabled, internally they remove the association of User with contact without running a trigger.
Can we still write trigger when customer user is diabled ? yes.
User object has a field named as IsPortalEnabled you can use it to your advantage. So your trigger will be like.
trigger User on User(after insert, after update) {
Set < Id > contactIds = new Set < Id > ();
for (User u: Trigger.new) {
if (u.IsPortalEnabled == false && Trigger.oldmap.get(u.id).IsPortalEnabled ==true) {
contactIds.add(Trigger.oldmap.get(u.id).ContactId);
}
if (contactIds.size() > 0) {
List < Contact > contacts = [select id, Has_Access_to_Customer_Portal__c from Contact where Id in: contactIds];
for (Contact c: contacts) {
c.Has_Access_to_Customer_Portal__c = false;
}
updateupdateContactsInFuture(JSON.serialize(contacts)); contacts;//Should use future. as its Mixed DML.https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_non_mix_sobjects.htm
}
}
}