0

This Trigger is being created to update a lookup field on Contact if the Contacts Community account is disabled.

I have a field on Contact called Support_Community_User__c that is a lookup to User which gets assigned the User.Id of the Contacts related Community User Id upon Community account creation. This is done via a separate Trigger.

What I am trying to accomplish with this Trigger is if we Disable a Customer User via the Manage External User Button on Contact which unchecks the Active checkbox on the related user and removes the relationship to the Contact in the Contact field on User, I want to remove the User.Id from the Support_Community_User__c field on Contact.

Here is my attempt that is not updating the Contact as I would expect and I am unsure why.

trigger CommunityUserContactRemove on User (after update) {

    Profile p = [SELECT Id FROM Profile WHERE Name = 'Quatro Community Login User'];

    List<Contact> userContacts = new List<Contact>();

    for (User u : trigger.new) {
        User oldUser = Trigger.oldMap.get(u.Id);
        if (u.ContactId == null & u.ProfileId == p.Id){
            userContacts.add(new Contact(
                    Id = oldUser.ContactId,
                    Support_Community_User__c = null));
        } 
    }

    update userContacts;
}
5
  • 1
    First use u.ContactId == null as it either is or is not. Are you debugging to ensure the contact id is actually null? I suspect you are not entering the if block. Also debug the userContacts list before update to see if it is empty. What else have you done to debug? Are any errors being thrown?
    – Eric
    Commented Jul 23, 2017 at 17:10
  • I changed the if statement to a simple one I knew should match if (u.CommunityNickname == 'goodnick') and that created an exception first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Contact, original object: User: []: ()
    – Jason
    Commented Jul 23, 2017 at 17:35
  • I will work on better debugging
    – Jason
    Commented Jul 23, 2017 at 17:35
  • Right. Figured you would get that error. Move the update to a future method
    – Eric
    Commented Jul 23, 2017 at 18:23
  • 1
    I still need to figure out why the if (u.ContactId == null & u.ProfileId == p.Id) statement does not seem to trigger, it should be true I will need to figure that out via debugging.
    – Jason
    Commented Jul 23, 2017 at 20:23

1 Answer 1

-2

@Jason, Try with the below code

trigger CommunityUserContactRemove on User (after update) {
    Profile p = [SELECT Id FROM Profile WHERE Name = 'Quatro Community Login User'];
    List<Contact> userContacts = new List<Contact>();
    List<Id> contactId= new list<Id>();
    for (User u : trigger.new) {
        User oldUser = Trigger.oldMap.get(u.Id);
        if (u.ContactId == null & u.ProfileId == p.Id){
            contactId.add(oldUser.ContactId);
        } 
    }
    for(Contact objCon:[SELECT Id,Support_Community_User__c  From contact where Id IN:contactId] ){
        objCon.Support_Community_User__c = null;
        userContacts.add(objCon);
    }
    if(userContacts <> NULL && !userContacts.isEmpty()){
        try{
            update userContacts;
         }
        catch(Exception e){
            system.debug('e--'+e);
         }
    }
 }
6
  • 1
    Please do not just dump code but rather include words which explain what you would change and why.
    – Adrian Larson
    Commented Jul 24, 2017 at 10:34
  • @AdrianLarson, The code which you have written is correct but there is syntax error in "if (u.ContactId == null & u.ProfileId == p.Id)" this statement. You should use && instead of &. '&' is bitwise & operator. You should use logical operator Commented Jul 24, 2017 at 13:46
  • Please edit your answer to reflect any clarifications. I have not written any code even peripherally related to this Q&A.
    – Adrian Larson
    Commented Jul 24, 2017 at 14:03
  • @sukanyabanekar per your comment should your code example use an && vs the & in the if (u.ContactId == null & u.ProfileId == p.Id) statement? Also your code example give me a compile error of Compile Error: Illegal assignment from List<Contact> to List<Id> at line 4
    – Jason
    Commented Jul 24, 2017 at 14:15
  • @AdrianLarson please use List<Id> contactId= new list<Id>(); Commented Jul 25, 2017 at 5:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .