1

I want to write a trigger that will mark a checkbox on the contact object once a user clicks on the 'Manage External Users'->'Disable Customer User' enter image description here

This is the trigger I wrote, it does not run when I click on the disable customer user:

trigger User on User(after insert, after update) {
        Set < Id > contactIds = new Set < Id > ();
        for (User u: Trigger.new) {
            if (u.ContactId == null && Trigger.oldmap.get(u.id).ContactId != null) {
                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;
            }
        }

I guess the answer would be that the trigger object should be a contact, but I don't really know how to work it out.

1 Answer 1

0

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
        }
    }
}
3
  • I'm getting MIXED_DML_OPERATION error, maybe I should use @future?
    – Json
    May 8, 2018 at 10:23
  • yeah,,, Its Setup Non Setup object. So yes. Future/queuable. Reaching this exception means your trigger is getting called and you are updating contact which was the original issue. May 8, 2018 at 10:34
  • using IsPortalEnabled (instead of isActive) and the future annotation did the trick, thank you @Pranay!
    – Json
    May 8, 2018 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.