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
NajoopNajoop 

Contact update trigger on account inactive

Still learning apex. trying to update all contacts for a Account if Account type field is set to customer-inactive. 

this does not work when account is updated but works when a contact is updated.

Not sure why. any help is appreciated.

 

trigger code below.

 

 

trigger updatefields on Account (after insert)

{

for (Account accs : Trigger.new)

 {

  if (accs.Account_type__C == 'Customer-Inactive' ) {

  Contact cons = [SELECT Id FROM Contact WHERE AccountId = :accs.ID];

  Cons.HasOptedOutOfEmail = True;

  update cons;

  }

  }

}

wesnoltewesnolte

Hey

 

This trigger will only fire after a record is inserted. You mention 'updating' in your message, for this you will need to use an after update trigger too.

 

Do you have any triggers on contact? 

hisrinuhisrinu

Try this, this will work out

 

trigger updatefields on Account (after insert, after update)

{

for (Account accs : Trigger.new)

 {

  if (accs.Account_type__C == 'Customer-Inactive' ) {

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

  cons = [SELECT Id FROM Contact WHERE AccountId = :accs.ID];

  for(Contact c: cons)

  c.HasOptedOutOfEmail = True;

  update cons;

  }

  }

}

Message Edited by hisrinu on 06-24-2009 04:22 AM
Kirtesh_JainKirtesh_Jain

hi,

 

yes you should use after update trigger.

 

If you are using trigger on  after insert  of account, then how you will get contacts of that Account.

those  contact will not available on  insert of account.

 

Thanks,

Kirtesh

NajoopNajoop

Thanks everyone. I tried that but it still does not work.

 

no trigger on contacts. I updated the contact record and the field is updated.

but not when account field is set to inactive.

 

any ideas!