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
MrBurnz1980MrBurnz1980 

Update Contact record from customer portal user record

I am trying to write a simple trigger that will update the contact record if a user updates their email address in the customer portal (User record). The code below works but I get a DML exception once the code is Active and a new contact is being enabled to be a customer portal user, exisiting customer portal user record is modified. What am I doing wrong? Thanks.

 

trigger ContactUpdater on User bulk (after update) { 
        Contact a; 
        for (User u : Trigger.new) { 
                String contactId = u.ContactId; 
                if (contactId!=null && contactId!='') { 
                        a = [select Id,Email from Contact where Id=:contactId]; 
                        a.Email = u.Email;       
                        update a; 
                } 
        }
}

 

Avidev9Avidev9
What is the exact error your getting?
MrBurnz1980MrBurnz1980

The trigger works but if for example I change the role of the customer portal user here is what the error I get.

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger update_contact caused an unexpected exception, contact your administrator: ContactUpdater: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 003J000000fVaF7IAK; 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: []: Trigger.update_contact: line 19, column 1

 

Avidev9Avidev9

Well ooopss! how can I missed that!

Anyways user is considered as a setup object and if you are doing any DML on setup object, another DML to a non setup Object in our case contact is not allowed. To make this work you can try using future methods.

 

So try some thing like this. Create a new Class

 

public class contactManager() {
    @future
    public static void updateContacts(Set < Id > userIds) {
        List < Contact > conList = new List < Contact > ();
        for (User u: [SELECT Id, Email, ContactId FROM USER WHERE Id IN: userIds and ContactId != NULL]) {
            Contact con = new Contact(Id = u.ContactId, Email = u.Email);
            conList.add(con);
        }

        update conList;
    }
}

 

Call the same from trigger

 

trigger ContactUpdater on User(after update) { 
       contactManager.updateContacts(trigger.newMap.keySet());
}

 To know more about future methods you can go through this link http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_future.htm

 

MrBurnz1980MrBurnz1980

Thanks I keep getting the following error on the trigger code

 

Variable does not exist: trigger	ContactUpdaterNew.trigger	/Update User Record on Contact Update/src/triggers	line 2	Force.com save problem

 

Avidev9Avidev9
There was a syntax error!
You should have caught It.

Edited my answer, changes are highlighted Please check that!