Skip to main content
added 141 characters in body
Source Link
Pranay Jaiswal
  • 36.4k
  • 17
  • 79
  • 138

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
        }
    }
}

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;
            }
            update contacts;
        }
    }
}

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;
            }
            updateContactsInFuture(JSON.serialize(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
        }
    }
}
Source Link
Pranay Jaiswal
  • 36.4k
  • 17
  • 79
  • 138

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;
            }
            update contacts;
        }
    }
}