You need to sign in to do that
Don't have an account?
Najoop
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;
}
}
}
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?
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;
}
}
}
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
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!