+ Start a Discussion

APEX: Check a contact also has an associated Customer Portal User



I am writing a trigger that updates a customer portal user when the associated contact is updated.  I have the class but only want to run the trigger when the contact has an associated cust portal user - I can't seem to find reference to this at all. y Trigger would be something like


trigger UpdateUserfromContact on Contact (after update) {


        Contact c =  Trigger.new[0];


        //And only if the contact has a related Customer Portal User associated








The contact appears as a lookup on the user record, thus you'd need to pull back the user detail based on the contact id:


List<User> users=[select id, ContactId from User where id = :c.id];

if (!users.isEmpty())
   User u = users[0];
   // do whatever here

 I'm also duty bound to point out that your trigger isn't bulkified, so it won't handle changes to a number of users at once, via the data loader for instance.