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

Best Answer chosen by Admin (Salesforce Developers) 
SurekaSureka
Hi,

Try this,

trigger UpdCustPortUser on Contact (after update)
{
List<User> usrs = [select Id, ByPassVR__c,ContactId from user where contactId in: trigger.new];
List<User> toUpd = new List<User>();
for(User u: usrs)
{
for(Contact c: trigger.new)
{
if(u.contactId == c.Id)
{
u.fax = '4566';
toUpd.add(u);
}
}
}
update toUpd;

}

Thanks

All Answers

SurekaSureka
Hi,

Try this,

trigger UpdCustPortUser on Contact (after update)
{
List<User> usrs = [select Id, ByPassVR__c,ContactId from user where contactId in: trigger.new];
List<User> toUpd = new List<User>();
for(User u: usrs)
{
for(Contact c: trigger.new)
{
if(u.contactId == c.Id)
{
u.fax = '4566';
toUpd.add(u);
}
}
}
update toUpd;

}

Thanks
This was selected as the best answer
lescclarkcpdlescclarkcpd

Fantastic thanks, I will update as the solutoin, however....

 

I now have 2 triggers, 1 that updates the contact when portal user updates & 1 that does the opposite - ie updates user when contact updated.  The idea is to keep the data nice and clean.  However, I am getting the follwoing message and not sure why - something to do with governor limits I guess. 

 

Error:Apex trigger UpdCustPortUserv2 caused an unexpected exception, contact your administrator: UpdCustPortUserv2: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 005M0000004PEK2IAO; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, NewUpdateContact: maximum trigger depth exceeded Contact trigger event AfterUpdate for [003M000000K2cBO] User trigger event AfterUpdate for [005M0000004PEK2] Contact trigger event AfterUpdate for [003M000000K2cBO] User trigger event AfterUpdate for [005M0000004PEK2] Contact trigger event AfterUpdate for [003M000000K2cBO] User trigger event AfterUpdate for [005M0000004PEK2] Contact trigger event AfterUpdate for [003M000000K2cBO] User trigger event AfterUpdate for [005M0000004PEK2] Contact trigger event AfterUpdate for [003M000000K2cBO] User trigger event AfterUpdate for [005M0000004PEK2] Contact trigger event AfterUpdate for [003M000000K2cBO] User trigger event AfterUpdate for [005M0000004PEK2] Contact trigger event AfterUpdate for [003M000000K2cBO] User trigger event AfterUpdate for [005M0000004PEK2] Contact trigger event AfterUpdate for [003M000000K2cBO] User trigger event AfterUpdate for [005M0000004PEK2]: []: Trigger.UpdCustPortUserv2: line 17, column 1

 

The triggers are:

trigger UpdCustPortUserv2 onContact (afterinsert, afterupdate

{

List<User> usrs = [select Id,ContactId from user where contactId in: trigger.new];

List<User> toUpd = new List<User>();

for(User u: usrs)

{

for(Contact c: trigger.new)

{

if(u.contactId == c.Id)

{

u.fax = '4566';

toUpd.add(u);

}

}

}

update toUpd;

}

 

 

trigger NewUpdateContact onUser (afterinsert, afterupdate) {

if (Trigger.new.size()==1) {      

User User =  Trigger.new[0];

if (User.ContactId!=null) {

Contact c = [select id from contact where id=:User.ContactId];

c.fax = user.fax;   

update c;

}

}

 

 

Can you help??

 

thanks

 

SurekaSureka
Hi,

Before updating the contact, try checking

if(c.fax != user.fax) , then update the contact.

As you are not checking that, trigger is repeatedly firing. Have the above condition.
lescclarkcpdlescclarkcpd

I understand but am actually updating a number of variables so would have to check all of them I guess, any other ideas