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
lescclarkcpdlescclarkcpd 

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

hi

 

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

        

            UpdatePortalUserfromContact.updateUser(c.Id);

      

}

 

thanks

bob_buzzardbob_buzzard

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.